package net.oschina.app.ui;

import greendroid.widget.MyQuickAction;
import greendroid.widget.QuickActionGrid;
import greendroid.widget.QuickActionWidget;
import greendroid.widget.QuickActionWidget.OnQuickActionClickListener;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.oschina.app.AppConfig;
import net.oschina.app.AppContext;
import net.oschina.app.AppException;
import net.oschina.app.AppManager;
import net.oschina.app.R;
import net.oschina.app.adapter.ListViewActiveAdapter;
import net.oschina.app.adapter.ListViewBlogAdapter;
import net.oschina.app.adapter.ListViewMessageAdapter;
import net.oschina.app.adapter.ListViewNewsAdapter;
import net.oschina.app.adapter.ListViewQuestionAdapter;
import net.oschina.app.adapter.ListViewTweetAdapter;
import net.oschina.app.api.ApiClient;
import net.oschina.app.bean.Active;
import net.oschina.app.bean.ActiveList;
import net.oschina.app.bean.Blog;
import net.oschina.app.bean.BlogList;
import net.oschina.app.bean.MessageList;
import net.oschina.app.bean.Messages;
import net.oschina.app.bean.News;
import net.oschina.app.bean.NewsList;
import net.oschina.app.bean.Notice;
import net.oschina.app.bean.Post;
import net.oschina.app.bean.PostList;
import net.oschina.app.bean.Result;
import net.oschina.app.bean.Tweet;
import net.oschina.app.bean.TweetList;
import net.oschina.app.common.StringUtils;
import net.oschina.app.common.UIHelper;
import net.oschina.app.common.UpdateManager;
import net.oschina.app.widget.BadgeView;
import net.oschina.app.widget.NewDataToast;
import net.oschina.app.widget.PullToRefreshListView;
import net.oschina.app.widget.ScrollLayout;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.TextView;

import com.jb.workflow.app.adapter.ListViewJBNewsAdapter;
import com.jb.workflow.app.adapter.ListViewInstAdapter;
import com.jb.workflow.app.adapter.ListViewTodoAdapter;
import com.jb.workflow.app.bean.InstanceList;
import com.jb.workflow.app.bean.JBNews;
import com.jb.workflow.app.bean.JBNewsList;
import com.jb.workflow.app.bean.ProcessInst;
import com.jb.workflow.app.bean.TodoList;
import com.jb.workflow.app.bean.TodoTask;

/**
 * 应用程序首页
 * 
 * @author liux (http://my.oschina.net/liux)
 * @version 1.0
 * @created 2012-3-21
 */
public class Main extends BaseActivity {
	
	public static final int QUICKACTION_LOGIN_OR_LOGOUT = 0;
	public static final int QUICKACTION_USERINFO = 5;
	public static final int QUICKACTION_SOFTWARE = 1;
	public static final int QUICKACTION_SEARCH = 2;
	public static final int QUICKACTION_SETTING = 3;
	public static final int QUICKACTION_EXIT = 4;

	private ScrollLayout mScrollLayout;
	private RadioButton[] mButtons;
	private String[] mHeadTitles;
	private int mViewCount;
	private int mCurSel;

	private ImageView mHeadLogo;
	private TextView mHeadTitle;
	private ProgressBar mHeadProgress;
	//顶部右侧的图片按钮，现在用来新增一个流程
	private ImageButton mHeadPubTask;
//	private ImageButton mHead_search;
//	private ImageButton mHeadPub_post;
//	private ImageButton mHeadPub_tweet;

	//当前显示的第几个类别（顶部RadioButton）
	//**8 
	private int curJBNewsCatalog = JBNewsList.CATALOG_RUNNING;
	private int curInstCatalog = InstanceList.CATALOG_RUNNING;
	private int curTodoCatalog = TodoTask.CATALOG_CLAIM;
	private int curNewsCatalog = NewsList.CATALOG_ALL;
	private int curQuestionCatalog = PostList.CATALOG_ASK;
	private int curTweetCatalog = TweetList.CATALOG_LASTEST;
	private int curActiveCatalog = ActiveList.CATALOG_LASTEST;

	//列表
	private PullToRefreshListView lvJBNews;
	private PullToRefreshListView lvInst;
	private PullToRefreshListView lvTodoTask;
	private PullToRefreshListView lvNews;
	private PullToRefreshListView lvBlog;
	private PullToRefreshListView lvQuestion;
	private PullToRefreshListView lvTweet;
	private PullToRefreshListView lvActive;
	private PullToRefreshListView lvMsg;

	//新增新的列表
	//**1
	private ListViewJBNewsAdapter lvJBNewsAdapter;
	private ListViewInstAdapter lvInstAdapter;
	private ListViewTodoAdapter lvTodoTaskAdapter;
	private ListViewNewsAdapter lvNewsAdapter;
	private ListViewBlogAdapter lvBlogAdapter;
	private ListViewQuestionAdapter lvQuestionAdapter;
	private ListViewTweetAdapter lvTweetAdapter;
	private ListViewActiveAdapter lvActiveAdapter;
	private ListViewMessageAdapter lvMsgAdapter;

	//**2
	private List<JBNews> lvJBNewsData = new ArrayList<JBNews>();
	private List<ProcessInst> lvInstData = new ArrayList<ProcessInst>();
	private List<TodoTask> lvTodoTaskData = new ArrayList<TodoTask>();
	private List<News> lvNewsData = new ArrayList<News>();
	private List<Blog> lvBlogData = new ArrayList<Blog>();
	private List<Post> lvQuestionData = new ArrayList<Post>();
	private List<Tweet> lvTweetData = new ArrayList<Tweet>();
	private List<Active> lvActiveData = new ArrayList<Active>();
	private List<Messages> lvMsgData = new ArrayList<Messages>();

	//listview的初始化Handler
	//**3
	private Handler lvJBNewsHandler;
	private Handler lvInstHandler;
	private Handler lvTodoTaskHandler;
	private Handler lvNewsHandler;
	private Handler lvBlogHandler;
	private Handler lvQuestionHandler;
	private Handler lvTweetHandler;
	private Handler lvActiveHandler;
	private Handler lvMsgHandler;

	//**4
	private int lvJBNewsSumData;
	private int lvInstSumData;
	private int lvTodoSumData;
	private int lvNewsSumData;
	private int lvBlogSumData;
	private int lvQuestionSumData;
	private int lvTweetSumData;
	private int lvActiveSumData;
	private int lvMsgSumData;
	
	//底部栏的按钮
	//for wf 不需要新加
	//Todo使用fbTweet，流程使用fbNews
	//新增的JBNews，使用Question？
	private RadioButton fbNews;
	private RadioButton fbQuestion;
	private RadioButton fbTweet;
	private RadioButton fbactive;
	private ImageView fbSetting;//单击弹出设置框

	//顶部类别的按钮
	//**5 顶部类别的按钮，如果没有呢，则不需要添加？
	private Button framebtn_Inst_running;
	private Button framebtn_Inst_complete;
	//for wf 需要新加
	private Button framebtn_TodoTask_claim;
	private Button framebtn_TodoTask_todo;
	private Button framebtn_TodoTask_all;
	private Button framebtn_News_lastest;
	private Button framebtn_News_blog;
	private Button framebtn_News_recommend;
	private Button framebtn_Question_ask;
	private Button framebtn_Question_share;
	private Button framebtn_Question_other;
	private Button framebtn_Question_job;
	private Button framebtn_Question_site;
	//动弹相关按钮
	private Button framebtn_Tweet_lastest;
	private Button framebtn_Tweet_hot;
	private Button framebtn_Tweet_my;
	private Button framebtn_Active_lastest;
	private Button framebtn_Active_atme;
	private Button framebtn_Active_comment;
	private Button framebtn_Active_myself;
	private Button framebtn_Active_message;

	//底部的view
	//**6 
	private View lvJBNews_footer;
	private View lvInst_footer;
	private View lvTodoTask_footer;
	private View lvNews_footer;
	private View lvBlog_footer;
	private View lvQuestion_footer;
	private View lvTweet_footer;
	private View lvActive_footer;
	private View lvMsg_footer;

	//**7 
	private TextView lvJBNews_foot_more;
	private TextView lvInst_foot_more; 
	private TextView lvTodoTask_foot_more;
	private TextView lvNews_foot_more;
	private TextView lvBlog_foot_more;
	private TextView lvQuestion_foot_more;
	private TextView lvTweet_foot_more;
	private TextView lvActive_foot_more;
	private TextView lvMsg_foot_more;

	//底部的更新进度条
	private ProgressBar lvJBNews_foot_progress;
	private ProgressBar lvInst_foot_progress;
	private ProgressBar lvTodoTask_foot_progress;	
	private ProgressBar lvNews_foot_progress;
	private ProgressBar lvBlog_foot_progress;
	private ProgressBar lvQuestion_foot_progress;
	private ProgressBar lvTweet_foot_progress;
	private ProgressBar lvActive_foot_progress;
	private ProgressBar lvMsg_foot_progress;

	public static BadgeView bv_active;
	public static BadgeView bv_message;
	public static BadgeView bv_atme;
	public static BadgeView bv_review;

	private QuickActionWidget mGrid;// 快捷栏控件

	private boolean isClearNotice = false;
	private int curClearNoticeType = 0;

	//private TweetReceiver tweetReceiver;// 动弹发布接收器
	private AppContext appContext;// 全局Context
	
	private DoubleClickExitHelper mDoubleClickExitHelper;
	
	/**表明最近是否被刷新过，是得话自动刷新会忽略一次*/
	private volatile boolean hasRefreshed = false;
	/**是否正在刷新，如果正在刷新则返回*/
	private volatile boolean isRefreshing = false;
	/**是否关闭自动刷新*/
	private volatile boolean autoRefresh = true;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		mDoubleClickExitHelper = new DoubleClickExitHelper(this);
		
		// 注册广播接收器
		//tweetReceiver = new TweetReceiver();
		//IntentFilter filter = new IntentFilter();
		//filter.addAction("net.oschina.app.action.APP_TWEETPUB");
		//registerReceiver(tweetReceiver, filter);

		appContext = (AppContext) getApplication();
		// 网络连接判断
		if (!appContext.isNetworkConnected())
			UIHelper.ToastMessage(this, R.string.network_not_connected);
		// 初始化登录
		appContext.initLoginInfo();//TODO：获取用户信息
		
		//for test
		//UIHelper.ToastMessage(this, "for test v1.2");

		this.initHeadView();
		this.initFootBar();
		this.initPageScroll();
		this.initFrameButton();
		this.initBadgeView();
		this.initQuickActionGrid();
		this.initFrameListView();

		// 检查新版本
		if (appContext.isCheckUp()) {
			UpdateManager.getUpdateManager().checkAppUpdate(this, false);
		}

		// 启动轮询通知信息
		//this.foreachUserNotice();
		this.foreachTodoTask();
		// ****检查是否需要下载欢迎图片
		this.checkNewOrderProcessDef();
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (mViewCount == 0)
			mViewCount = 4;
		if (mCurSel == 0 && !fbTweet.isChecked()) {
			fbNews.setChecked(false);
			fbQuestion.setChecked(false);
			fbTweet.setChecked(true);
			fbactive.setChecked(false);
		}
		// 读取左右滑动配置
		mScrollLayout.setIsScroll(appContext.isScroll());
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		//unregisterReceiver(tweetReceiver);
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);

		if (intent.getBooleanExtra("LOGIN", false)) {
			// 加载动弹、动态及留言(当前动弹的catalog大于0表示用户的uid)
			
			if (lvTodoTaskData.isEmpty() && mCurSel == 0) {
				loadLvTodoTaskData(curTodoCatalog, 0, lvTodoTaskHandler,
						UIHelper.LISTVIEW_ACTION_INIT);
			} else if (lvInstData.isEmpty() && mCurSel == 1) {
				loadLvInstData(curInstCatalog, 0, lvInstHandler,
						UIHelper.LISTVIEW_ACTION_INIT);
			}
			
//			if (lvTweetData.isEmpty() && curTweetCatalog > 0 && mCurSel == 2) {
//				this.loadLvTweetData(curTweetCatalog, 0, lvTweetHandler,UIHelper.LISTVIEW_ACTION_INIT);
//			} else if (mCurSel == 3) {
//				if (lvActiveData.isEmpty()) {
//					this.loadLvActiveData(curActiveCatalog, 0, lvActiveHandler,UIHelper.LISTVIEW_ACTION_INIT);
//				}
//				if (lvMsgData.isEmpty()) {
//					this.loadLvMsgData(0, lvMsgHandler,UIHelper.LISTVIEW_ACTION_INIT);
//				}
//			}
		} else if (intent.getBooleanExtra("NOTICE", false)) {
			// 查看最新信息
			//mScrollLayout.scrollToScreen(3);
		} else if (intent.getBooleanExtra("TASK", false)) {
			// 查看task最新信息
			mScrollLayout.scrollToScreen(0);
		}
	}

//	public class TweetReceiver extends BroadcastReceiver {
//		@Override
//		public void onReceive(final Context context, Intent intent) {
//			int what = intent.getIntExtra("MSG_WHAT", 0);
//			if (what == 1) {
//				Result res = (Result) intent.getSerializableExtra("RESULT");
//				UIHelper.ToastMessage(context, res.getErrorMessage(), 1000);
//				if (res.OK()) {
//					// 发送通知广播
//					if (res.getNotice() != null) {
//						UIHelper.sendBroadCast(context, res.getNotice());
//					}
//					// 发完动弹后-刷新最新动弹、我的动弹&最新动态(当前界面必须是动弹|动态)
//					if (curTweetCatalog >= 0 && mCurSel == 2) {
//						loadLvTweetData(curTweetCatalog, 0, lvTweetHandler,UIHelper.LISTVIEW_ACTION_REFRESH);
//					} else if (curActiveCatalog == ActiveList.CATALOG_LASTEST&& mCurSel == 3) {
//						
//						loadLvActiveData(curActiveCatalog, 0, lvActiveHandler,UIHelper.LISTVIEW_ACTION_REFRESH);
//					}
//				}
//			} else {
//				final Tweet tweet = (Tweet) intent
//						.getSerializableExtra("TWEET");
//				final Handler handler = new Handler() {
//					public void handleMessage(Message msg) {
//						if (msg.what == 1) {
//							Result res = (Result) msg.obj;
//							UIHelper.ToastMessage(context,res.getErrorMessage(), 1000);
//							if (res.OK()) {
//								// 发送通知广播
//								if (res.getNotice() != null) {
//									UIHelper.sendBroadCast(context,res.getNotice());
//								}
//								// 发完动弹后-刷新最新、我的动弹&最新动态
//								if (curTweetCatalog >= 0 && mCurSel == 2) {
//									loadLvTweetData(curTweetCatalog, 0,lvTweetHandler,UIHelper.LISTVIEW_ACTION_REFRESH);
//								} else if (curActiveCatalog == ActiveList.CATALOG_LASTEST&& mCurSel == 3) {
//									loadLvActiveData(curActiveCatalog, 0,lvActiveHandler,UIHelper.LISTVIEW_ACTION_REFRESH);
//								}
//								if (TweetPub.mContext != null) {
//									// 清除动弹保存的临时编辑内容
//									appContext.removeProperty(AppConfig.TEMP_TWEET,AppConfig.TEMP_TWEET_IMAGE);
//									((Activity) TweetPub.mContext).finish();
//								}
//							}
//						} else {
//							((AppException) msg.obj).makeToast(context);
//							if (TweetPub.mContext != null&&TweetPub.mMessage != null)
//								TweetPub.mMessage.setVisibility(View.GONE);
//						}
//					}
//				};
//				Thread thread = new Thread() {
//					public void run() {
//						Message msg = new Message();
//						try {
//							Result res = appContext.pubTweet(tweet);
//							msg.what = 1;
//							msg.obj = res;
//						} catch (AppException e) {
//							e.printStackTrace();
//							msg.what = -1;
//							msg.obj = e;
//						}
//						handler.sendMessage(msg);
//					}
//				};
//				if (TweetPub.mContext != null)
//					UIHelper.showResendTweetDialog(TweetPub.mContext, thread);
//				else
//					UIHelper.showResendTweetDialog(context, thread);
//			}
//		}
//	}

	/**
	 * 初始化快捷栏
	 * ****退出/登陆
	 */
	private void initQuickActionGrid() {
		mGrid = new QuickActionGrid(this);
		mGrid.addQuickAction(new MyQuickAction(this, R.drawable.ic_menu_login,
				R.string.main_menu_login));
//		mGrid.addQuickAction(new MyQuickAction(this, R.drawable.ic_menu_myinfo,
//				R.string.main_menu_myinfo));
		mGrid.addQuickAction(new MyQuickAction(this,
				R.drawable.ic_menu_software, R.string.main_menu_stop_auto_refresh));
		mGrid.addQuickAction(new MyQuickAction(this, R.drawable.ic_menu_sweep,
				R.string.main_menu_sweep));
		mGrid.addQuickAction(new MyQuickAction(this,
				R.drawable.ic_menu_setting, R.string.main_menu_setting));
		mGrid.addQuickAction(new MyQuickAction(this, R.drawable.ic_menu_exit,
				R.string.main_menu_exit));

		mGrid.setOnQuickActionClickListener(mActionListener);
	}

	/**
	 * 快捷栏item点击事件
	 */
	private OnQuickActionClickListener mActionListener = new OnQuickActionClickListener() {
		public void onQuickActionClicked(QuickActionWidget widget, int position) {
			switch (position) {
			case QUICKACTION_LOGIN_OR_LOGOUT:// 用户登录-注销
				UIHelper.loginOrLogout(Main.this);
				break;
			case QUICKACTION_USERINFO:// 我的资料
				//UIHelper.showUserInfo(Main.this);
				break;
			case QUICKACTION_SOFTWARE:// 开源软件
				autoRefresh = !autoRefresh;
				UIHelper.ToastMessage(Main.this, autoRefresh ? "自动刷新已开启" : "自动刷新已关闭");
				//UIHelper.showSoftware(Main.this);
				break;
			case QUICKACTION_SEARCH:// 
				UIHelper.showCapture(Main.this);
				break;
			case QUICKACTION_SETTING:// 设置
				UIHelper.showSetting(Main.this);
				break;
			case QUICKACTION_EXIT:// 退出
				AppManager.getAppManager().finishActivity(Main.this);
				break;
			}
		}
	};
	
	private void checkNewOrderProcessDef() {
		if (!appContext.isNetworkConnected()) {
			return;
		}
		// 启动线程去检查服务器接口是否需要下载新的最新订单流程列表到手机
		new Thread(){
			public void run() {
				// 将图片下载下来
				try {
					ApiClient.checkNewProcessDef(appContext);
				} catch (AppException e) {
				}
			}
		}.start();
	}
	
//	private void checkBackGround() {
//		if (!appContext.isNetworkConnected()) {
//			return;
//		}
//		// 启动线程去检查服务器接口是否需要下载新的欢迎界面背景图片到手机
//		new Thread(){
//			public void run() {
//				// 将图片下载下来
//				try {
//					ApiClient.checkBackGround(appContext);
//				} catch (AppException e) {
//				}
//			}
//		}.start();
//	}

	/**
	 * 初始化所有ListView
	 */
	private void initFrameListView() {
		// 初始化listview控件
		this.initInstListView();
		this.initTodoTaskListView();
		this.initJBNewsListView();
		this.initNewsListView();
		this.initBlogListView();
		this.initQuestionListView();
		this.initTweetListView();
		this.initActiveListView();
		this.initMsgListView();
		// 加载listview数据
		this.initFrameListViewData();
	}

	/**
	 * 初始化所有ListView数据
	 */
	private void initFrameListViewData() {
		// 初始化Handler
		
		lvJBNewsHandler = this.getLvHandler(lvJBNews, lvJBNewsAdapter,
				lvJBNews_foot_more, lvJBNews_foot_progress,
				AppContext.PAGE_SIZE);
		lvInstHandler = this.getLvHandler(lvInst, lvInstAdapter,
				lvInst_foot_more, lvInst_foot_progress,
				AppContext.PAGE_SIZE);
		lvTodoTaskHandler = this.getLvHandler(lvTodoTask, lvTodoTaskAdapter,
				lvTodoTask_foot_more, lvTodoTask_foot_progress,
				AppContext.PAGE_SIZE);
		lvNewsHandler = this.getLvHandler(lvNews, lvNewsAdapter,
				lvNews_foot_more, lvNews_foot_progress, AppContext.PAGE_SIZE);
		lvBlogHandler = this.getLvHandler(lvBlog, lvBlogAdapter,
				lvBlog_foot_more, lvBlog_foot_progress, AppContext.PAGE_SIZE);
		lvQuestionHandler = this.getLvHandler(lvQuestion, lvQuestionAdapter,
				lvQuestion_foot_more, lvQuestion_foot_progress,
				AppContext.PAGE_SIZE);
		lvTweetHandler = this.getLvHandler(lvTweet, lvTweetAdapter,
				lvTweet_foot_more, lvTweet_foot_progress, AppContext.PAGE_SIZE);
		lvActiveHandler = this.getLvHandler(lvActive, lvActiveAdapter,
				lvActive_foot_more, lvActive_foot_progress,
				AppContext.PAGE_SIZE);
		lvMsgHandler = this.getLvHandler(lvMsg, lvMsgAdapter, lvMsg_foot_more,
				lvMsg_foot_progress, AppContext.PAGE_SIZE);

		// 加载初始化数据，必须登录
		if (!appContext.isLogin()) {
			UIHelper.showLoginDialog(Main.this);
		} else {
			if (lvTodoTaskData.isEmpty()) {
				loadLvTodoTaskData(curTodoCatalog, 0, lvTodoTaskHandler,
						UIHelper.LISTVIEW_ACTION_INIT);
			}
		}
	}

	/**
	 * 初始化新闻列表
	 */
	private void initNewsListView() {
		lvNewsAdapter = new ListViewNewsAdapter(this, lvNewsData,
				R.layout.news_listitem);
		lvNews_footer = getLayoutInflater().inflate(R.layout.listview_footer,
				null);
		lvNews_foot_more = (TextView) lvNews_footer
				.findViewById(R.id.listview_foot_more);
		lvNews_foot_progress = (ProgressBar) lvNews_footer
				.findViewById(R.id.listview_foot_progress);
		lvNews = (PullToRefreshListView) findViewById(R.id.frame_listview_news);
		lvNews.addFooterView(lvNews_footer);// 添加底部视图 必须在setAdapter前
		lvNews.setAdapter(lvNewsAdapter);
		lvNews.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// 点击头部、底部栏无效
				if (position == 0 || view == lvNews_footer)
					return;

				News news = null;
				// 判断是否是TextView
				if (view instanceof TextView) {
					news = (News) view.getTag();
				} else {
					TextView tv = (TextView) view
							.findViewById(R.id.news_listitem_title);
					news = (News) tv.getTag();
				}
				if (news == null)
					return;

				// 跳转到新闻详情
				UIHelper.showNewsRedirect(view.getContext(), news);
			}
		});
		lvNews.setOnScrollListener(new AbsListView.OnScrollListener() {
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				lvNews.onScrollStateChanged(view, scrollState);

				// 数据为空--不用继续下面代码了
				if (lvNewsData.isEmpty())
					return;

				// 判断是否滚动到底部
				boolean scrollEnd = false;
				try {
					if (view.getPositionForView(lvNews_footer) == view
							.getLastVisiblePosition())
						scrollEnd = true;
				} catch (Exception e) {
					scrollEnd = false;
				}

				int lvDataState = StringUtils.toInt(lvNews.getTag());
				if (scrollEnd && lvDataState == UIHelper.LISTVIEW_DATA_MORE) {
					lvNews.setTag(UIHelper.LISTVIEW_DATA_LOADING);
					lvNews_foot_more.setText(R.string.load_ing);
					lvNews_foot_progress.setVisibility(View.VISIBLE);
					// 当前pageIndex
					int pageIndex = lvNewsSumData / AppContext.PAGE_SIZE;
					loadLvNewsData(curNewsCatalog, pageIndex, lvNewsHandler,
							UIHelper.LISTVIEW_ACTION_SCROLL);
				}
			}

			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				lvNews.onScroll(view, firstVisibleItem, visibleItemCount,
						totalItemCount);
			}
		});
		lvNews.setOnRefreshListener(new PullToRefreshListView.OnRefreshListener() {
			public void onRefresh() {
				loadLvNewsData(curNewsCatalog, 0, lvNewsHandler,
						UIHelper.LISTVIEW_ACTION_REFRESH);
			}
		});
	}

	/**
	 * 初始化博客列表
	 */
	private void initBlogListView() {
		lvBlogAdapter = new ListViewBlogAdapter(this, BlogList.CATALOG_LATEST,
				lvBlogData, R.layout.blog_listitem);
		lvBlog_footer = getLayoutInflater().inflate(R.layout.listview_footer,
				null);
		lvBlog_foot_more = (TextView) lvBlog_footer
				.findViewById(R.id.listview_foot_more);
		lvBlog_foot_progress = (ProgressBar) lvBlog_footer
				.findViewById(R.id.listview_foot_progress);
		lvBlog = (PullToRefreshListView) findViewById(R.id.frame_listview_blog);
		lvBlog.addFooterView(lvBlog_footer);// 添加底部视图 必须在setAdapter前
		lvBlog.setAdapter(lvBlogAdapter);
		lvBlog.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// 点击头部、底部栏无效
				if (position == 0 || view == lvBlog_footer)
					return;

				Blog blog = null;
				// 判断是否是TextView
				if (view instanceof TextView) {
					blog = (Blog) view.getTag();
				} else {
					TextView tv = (TextView) view
							.findViewById(R.id.blog_listitem_title);
					blog = (Blog) tv.getTag();
				}
				if (blog == null)
					return;

				// 跳转到博客详情
				UIHelper.showUrlRedirect(view.getContext(), blog.getUrl());
			}
		});
		lvBlog.setOnScrollListener(new AbsListView.OnScrollListener() {
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				lvBlog.onScrollStateChanged(view, scrollState);

				// 数据为空--不用继续下面代码了
				if (lvBlogData.isEmpty())
					return;

				// 判断是否滚动到底部
				boolean scrollEnd = false;
				try {
					if (view.getPositionForView(lvBlog_footer) == view
							.getLastVisiblePosition())
						scrollEnd = true;
				} catch (Exception e) {
					scrollEnd = false;
				}

				int lvDataState = StringUtils.toInt(lvBlog.getTag());
				if (scrollEnd && lvDataState == UIHelper.LISTVIEW_DATA_MORE) {
					lvBlog.setTag(UIHelper.LISTVIEW_DATA_LOADING);
					lvBlog_foot_more.setText(R.string.load_ing);
					lvBlog_foot_progress.setVisibility(View.VISIBLE);
					// 当前pageIndex
					int pageIndex = lvBlogSumData / AppContext.PAGE_SIZE;
					loadLvBlogData(curNewsCatalog, pageIndex, lvBlogHandler,
							UIHelper.LISTVIEW_ACTION_SCROLL);
				}
			}

			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				lvBlog.onScroll(view, firstVisibleItem, visibleItemCount,
						totalItemCount);
			}
		});
		lvBlog.setOnRefreshListener(new PullToRefreshListView.OnRefreshListener() {
			public void onRefresh() {
				loadLvBlogData(curNewsCatalog, 0, lvBlogHandler,
						UIHelper.LISTVIEW_ACTION_REFRESH);
			}
		});
	}
	
	/**
	 * 初始化JBNews列表
	 */
	private void initJBNewsListView() {
		lvJBNewsAdapter = new ListViewJBNewsAdapter(this, lvJBNewsData,
				R.layout.jbnews_listitem);
		lvJBNews_footer = getLayoutInflater().inflate(
				R.layout.listview_footer, null);
		lvJBNews_foot_more = (TextView) lvJBNews_footer
				.findViewById(R.id.listview_foot_more);
		lvJBNews_foot_progress = (ProgressBar) lvJBNews_footer
				.findViewById(R.id.listview_foot_progress);
		lvJBNews = (PullToRefreshListView) findViewById(R.id.frame_listview_jbnews);
		lvJBNews.addFooterView(lvJBNews_footer);// 添加底部视图 必须在setAdapter前
		lvJBNews.setAdapter(lvJBNewsAdapter);
		lvJBNews.setOnItemClickListener(new AdapterView.OnItemClickListener() {
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {
						// 点击头部、底部栏无效
						if (position == 0 || view == lvJBNews_footer)
							return;

						JBNews JBNews = null;
						// 判断是否是TextView
						if (view instanceof TextView) {
							JBNews = (JBNews) view.getTag();
						} else {
							TextView tv = (TextView) view
									.findViewById(R.id.jbnews_listitem_title);
							JBNews = (JBNews) tv.getTag();
						}
						if (JBNews == null)
							return;

						// ****跳转到任务详情
						UIHelper.showJBNewsDetail(Main.this, JBNews);
					}
				});
		lvJBNews.setOnScrollListener(new AbsListView.OnScrollListener() {
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				lvJBNews.onScrollStateChanged(view, scrollState);

				// 数据为空--不用继续下面代码了
				if (lvJBNewsData.isEmpty())
					return;

				// 判断是否滚动到底部
				boolean scrollEnd = false;
				try {
					if (view.getPositionForView(lvJBNews_footer) == view
							.getLastVisiblePosition())
						scrollEnd = true;
				} catch (Exception e) {
					scrollEnd = false;
				}

				int lvDataState = StringUtils.toInt(lvJBNews.getTag());
				if (scrollEnd && lvDataState == UIHelper.LISTVIEW_DATA_MORE) {
					lvJBNews.setTag(UIHelper.LISTVIEW_DATA_LOADING);
					lvJBNews_foot_more.setText(R.string.load_ing);
					lvJBNews_foot_progress.setVisibility(View.VISIBLE);
					// 当前pageIndex
					int pageIndex = lvJBNewsSumData;
					loadLvJBNewsData(curJBNewsCatalog, pageIndex, lvJBNewsHandler, UIHelper.LISTVIEW_ACTION_SCROLL);
				}
			}

			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				lvJBNews.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
						
			}
		});
		lvJBNews.setOnRefreshListener(new PullToRefreshListView.OnRefreshListener() {
					public void onRefresh() {
						loadLvJBNewsData(curJBNewsCatalog, 0, lvJBNewsHandler, UIHelper.LISTVIEW_ACTION_REFRESH);
					}
				});
	}
	
	/**
	 * 初始化Inst列表
	 */
	private void initInstListView() {
		lvInstAdapter = new ListViewInstAdapter(this, lvInstData,
				R.layout.inst_listitem);
		lvInst_footer = getLayoutInflater().inflate(
				R.layout.listview_footer, null);
		lvInst_foot_more = (TextView) lvInst_footer
				.findViewById(R.id.listview_foot_more);
		lvInst_foot_progress = (ProgressBar) lvInst_footer
				.findViewById(R.id.listview_foot_progress);
		lvInst = (PullToRefreshListView) findViewById(R.id.frame_listview_processinst);
		lvInst.addFooterView(lvInst_footer);// 添加底部视图 必须在setAdapter前
		lvInst.setAdapter(lvInstAdapter);
		lvInst.setOnItemClickListener(new AdapterView.OnItemClickListener() {
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {
						// 点击头部、底部栏无效
						if (position == 0 || view == lvInst_footer)
							return;

						ProcessInst inst = null;
						// 判断是否是TextView
						if (view instanceof TextView) {
							inst = (ProcessInst) view.getTag();
						} else {
							TextView tv = (TextView) view
									.findViewById(R.id.inst_listitem_title);
							inst = (ProcessInst) tv.getTag();
						}
						if (inst == null)
							return;

						// ****跳转到任务详情
						UIHelper.showInstDetail(Main.this, inst);
					}
				});
		lvInst.setOnScrollListener(new AbsListView.OnScrollListener() {
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				lvInst.onScrollStateChanged(view, scrollState);

				// 数据为空--不用继续下面代码了
				if (lvInstData.isEmpty())
					return;

				// 判断是否滚动到底部
				boolean scrollEnd = false;
				try {
					if (view.getPositionForView(lvInst_footer) == view
							.getLastVisiblePosition())
						scrollEnd = true;
				} catch (Exception e) {
					scrollEnd = false;
				}

				int lvDataState = StringUtils.toInt(lvInst.getTag());
				if (scrollEnd && lvDataState == UIHelper.LISTVIEW_DATA_MORE) {
					lvInst.setTag(UIHelper.LISTVIEW_DATA_LOADING);
					lvInst_foot_more.setText(R.string.load_ing);
					lvInst_foot_progress.setVisibility(View.VISIBLE);
					// 当前pageIndex
					int pageIndex = lvInstSumData;
					loadLvInstData(curInstCatalog, pageIndex, lvInstHandler, UIHelper.LISTVIEW_ACTION_SCROLL);
				}
			}

			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				lvInst.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
						
			}
		});
		lvInst.setOnRefreshListener(new PullToRefreshListView.OnRefreshListener() {
					public void onRefresh() {
						loadLvInstData(curInstCatalog, 0, lvInstHandler, UIHelper.LISTVIEW_ACTION_REFRESH);
					}
				});
	}
	
	/**
	 * 初始化TodoTask列表
	 */
	private void initTodoTaskListView() {
		lvTodoTaskAdapter = new ListViewTodoAdapter(this, lvTodoTaskData,
				R.layout.todo_listitem);
		lvTodoTask_footer = getLayoutInflater().inflate(
				R.layout.listview_footer, null);
		lvTodoTask_foot_more = (TextView) lvTodoTask_footer
				.findViewById(R.id.listview_foot_more);
		lvTodoTask_foot_progress = (ProgressBar) lvTodoTask_footer
				.findViewById(R.id.listview_foot_progress);
		lvTodoTask = (PullToRefreshListView) findViewById(R.id.frame_listview_todotask);
		lvTodoTask.addFooterView(lvTodoTask_footer);// 添加底部视图 必须在setAdapter前
		lvTodoTask.setAdapter(lvTodoTaskAdapter);
		lvTodoTask
				.setOnItemClickListener(new AdapterView.OnItemClickListener() {
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {
						// 点击头部、底部栏无效
						if (position == 0 || view == lvTodoTask_footer)
							return;

						TodoTask task = null;
						// 判断是否是TextView
						if (view instanceof TextView) {
							task = (TodoTask) view.getTag();
						} else {
							TextView tv = (TextView) view
									.findViewById(R.id.todo_listitem_title);
							task = (TodoTask) tv.getTag();
						}
						if (task == null)
							return;

						// 跳转到任务详情
						UIHelper.showTodoTaskDetail(Main.this, task);
					}
				});
		lvTodoTask.setOnScrollListener(new AbsListView.OnScrollListener() {
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				lvTodoTask.onScrollStateChanged(view, scrollState);

				// 数据为空--不用继续下面代码了
				if (lvTodoTaskData.isEmpty())
					return;

				// 判断是否滚动到底部
				boolean scrollEnd = false;
				try {
					if (view.getPositionForView(lvTodoTask_footer) == view
							.getLastVisiblePosition())
						scrollEnd = true;
				} catch (Exception e) {
					scrollEnd = false;
				}

				int lvDataState = StringUtils.toInt(lvTodoTask.getTag());
				if (scrollEnd && lvDataState == UIHelper.LISTVIEW_DATA_MORE) {
					lvTodoTask.setTag(UIHelper.LISTVIEW_DATA_LOADING);
					lvTodoTask_foot_more.setText(R.string.load_ing);
					lvTodoTask_foot_progress.setVisibility(View.VISIBLE);
					// 当前pageIndex
					int pageIndex = lvTodoSumData;
					loadLvTodoTaskData(curTodoCatalog, pageIndex, lvTodoTaskHandler, UIHelper.LISTVIEW_ACTION_SCROLL);
				}
			}

			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				lvTodoTask.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
						
			}
		});
		lvTodoTask.setOnRefreshListener(new PullToRefreshListView.OnRefreshListener() {
					public void onRefresh() {
						loadLvTodoTaskData(curTodoCatalog, 0, lvTodoTaskHandler, UIHelper.LISTVIEW_ACTION_REFRESH);
					}
				});
	}

	/**
	 * 初始化帖子列表
	 */
	private void initQuestionListView() {
		lvQuestionAdapter = new ListViewQuestionAdapter(this, lvQuestionData,
				R.layout.question_listitem);
		lvQuestion_footer = getLayoutInflater().inflate(
				R.layout.listview_footer, null);
		lvQuestion_foot_more = (TextView) lvQuestion_footer
				.findViewById(R.id.listview_foot_more);
		lvQuestion_foot_progress = (ProgressBar) lvQuestion_footer
				.findViewById(R.id.listview_foot_progress);
		lvQuestion = (PullToRefreshListView) findViewById(R.id.frame_listview_question);
		lvQuestion.addFooterView(lvQuestion_footer);// 添加底部视图 必须在setAdapter前
		lvQuestion.setAdapter(lvQuestionAdapter);
		lvQuestion
				.setOnItemClickListener(new AdapterView.OnItemClickListener() {
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {
						// 点击头部、底部栏无效
						if (position == 0 || view == lvQuestion_footer)
							return;

						Post post = null;
						// 判断是否是TextView
						if (view instanceof TextView) {
							post = (Post) view.getTag();
						} else {
							TextView tv = (TextView) view
									.findViewById(R.id.question_listitem_title);
							post = (Post) tv.getTag();
						}
						if (post == null)
							return;

						// 跳转到问答详情
						UIHelper.showQuestionDetail(view.getContext(),
								post.getId());
					}
				});
		lvQuestion.setOnScrollListener(new AbsListView.OnScrollListener() {
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				lvQuestion.onScrollStateChanged(view, scrollState);

				// 数据为空--不用继续下面代码了
				if (lvQuestionData.isEmpty())
					return;

				// 判断是否滚动到底部
				boolean scrollEnd = false;
				try {
					if (view.getPositionForView(lvQuestion_footer) == view
							.getLastVisiblePosition())
						scrollEnd = true;
				} catch (Exception e) {
					scrollEnd = false;
				}

				int lvDataState = StringUtils.toInt(lvQuestion.getTag());
				if (scrollEnd && lvDataState == UIHelper.LISTVIEW_DATA_MORE) {
					lvQuestion.setTag(UIHelper.LISTVIEW_DATA_LOADING);
					lvQuestion_foot_more.setText(R.string.load_ing);
					lvQuestion_foot_progress.setVisibility(View.VISIBLE);
					// 当前pageIndex
					int pageIndex = lvQuestionSumData / AppContext.PAGE_SIZE;
					loadLvQuestionData(curQuestionCatalog, pageIndex,lvQuestionHandler, UIHelper.LISTVIEW_ACTION_SCROLL);
							
				}
			}

			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				lvQuestion.onScroll(view, firstVisibleItem, visibleItemCount,totalItemCount);
						
			}
		});
		lvQuestion
				.setOnRefreshListener(new PullToRefreshListView.OnRefreshListener() {
					public void onRefresh() {
						loadLvQuestionData(curQuestionCatalog, 0,lvQuestionHandler,UIHelper.LISTVIEW_ACTION_REFRESH);
								
					}
				});
	}

	/**
	 * 初始化动弹列表
	 */
	private void initTweetListView() {
		lvTweetAdapter = new ListViewTweetAdapter(this, lvTweetData,R.layout.tweet_listitem);
		lvTweet_footer = getLayoutInflater().inflate(R.layout.listview_footer,null);
		lvTweet_foot_more = (TextView) lvTweet_footer.findViewById(R.id.listview_foot_more);
		lvTweet_foot_progress = (ProgressBar) lvTweet_footer.findViewById(R.id.listview_foot_progress);
		lvTweet = (PullToRefreshListView) findViewById(R.id.frame_listview_tweet);
		lvTweet.addFooterView(lvTweet_footer);// 添加底部视图 必须在setAdapter前
		lvTweet.setAdapter(lvTweetAdapter);
		lvTweet.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, final View view,
					int position, long id) {
				// 点击头部、底部栏无效
				if (position == 0 || view == lvTweet_footer)
					return;

				Tweet tweet = null;
				// 判断是否是TextView
				if (view instanceof TextView) {
					tweet = (Tweet) view.getTag();
				} else {
					TextView tv = (TextView) view
							.findViewById(R.id.tweet_listitem_username);
					tweet = (Tweet) tv.getTag();
				}
				if (tweet == null)
					return;   			
				// 跳转到动弹详情&评论页面
				UIHelper.showTweetDetail(view.getContext(), tweet.getId());
			}
		});
		lvTweet.setOnScrollListener(new AbsListView.OnScrollListener() {
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				lvTweet.onScrollStateChanged(view, scrollState);

				// 数据为空--不用继续下面代码了
				if (lvTweetData.isEmpty())
					return;

				// 判断是否滚动到底部
				boolean scrollEnd = false;
				try {
					if (view.getPositionForView(lvTweet_footer) == view.getLastVisiblePosition())
						scrollEnd = true;
				} catch (Exception e) {
					scrollEnd = false;
				}

				int lvDataState = StringUtils.toInt(lvTweet.getTag());
				if (scrollEnd && lvDataState == UIHelper.LISTVIEW_DATA_MORE) {
					lvTweet.setTag(UIHelper.LISTVIEW_DATA_LOADING);
					lvTweet_foot_more.setText(R.string.load_ing);
					lvTweet_foot_progress.setVisibility(View.VISIBLE);
					// 当前pageIndex
					int pageIndex = lvTweetSumData / AppContext.PAGE_SIZE;
					loadLvTweetData(curTweetCatalog, pageIndex, lvTweetHandler,UIHelper.LISTVIEW_ACTION_SCROLL);
				}
			}

			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				lvTweet.onScroll(view, firstVisibleItem, visibleItemCount,
						totalItemCount);
			}
		});
		lvTweet.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				// 点击头部、底部栏无效
				if (position == 0 || view == lvTweet_footer)
					return false;

				Tweet _tweet = null;
				// 判断是否是TextView
				if (view instanceof TextView) {
					_tweet = (Tweet) view.getTag();
				} else {
					TextView tv = (TextView) view.findViewById(R.id.tweet_listitem_username);
					_tweet = (Tweet) tv.getTag();
				}
				if (_tweet == null)
					return false;

				final Tweet tweet = _tweet;

				// 删除操作
				// if(appContext.getLoginUid() == tweet.getAuthorId()) {
				final Handler handler = new Handler() {
					public void handleMessage(Message msg) {
						if (msg.what == 1) {
							Result res = (Result) msg.obj;
							if (res.OK()) {
								lvTweetData.remove(tweet);
								lvTweetAdapter.notifyDataSetChanged();
							}
							UIHelper.ToastMessage(Main.this,
									res.getErrorMessage());
						} else {
							((AppException) msg.obj).makeToast(Main.this);
						}
					}
				};
				Thread thread = new Thread() {
					public void run() {
						Message msg = new Message();
						try {
							Result res = appContext.delTweet(
									appContext.getLoginUid(), tweet.getId());
							msg.what = 1;
							msg.obj = res;
						} catch (AppException e) {
							e.printStackTrace();
							msg.what = -1;
							msg.obj = e;
						}
						handler.sendMessage(msg);
					}
				};
				UIHelper.showTweetOptionDialog(Main.this, thread);
				// } else {
				// UIHelper.showTweetOptionDialog(Main.this, null);
				// }
				return true;
			}
		});
		lvTweet.setOnRefreshListener(new PullToRefreshListView.OnRefreshListener() {
			public void onRefresh() {
				loadLvTweetData(curTweetCatalog, 0, lvTweetHandler,
						UIHelper.LISTVIEW_ACTION_REFRESH);
			}
		});
	}

	/**
	 * 初始化动态列表
	 */
	private void initActiveListView() {
		lvActiveAdapter = new ListViewActiveAdapter(this, lvActiveData,
				R.layout.active_listitem);
		lvActive_footer = getLayoutInflater().inflate(R.layout.listview_footer,
				null);
		lvActive_foot_more = (TextView) lvActive_footer
				.findViewById(R.id.listview_foot_more);
		lvActive_foot_progress = (ProgressBar) lvActive_footer
				.findViewById(R.id.listview_foot_progress);
		lvActive = (PullToRefreshListView) findViewById(R.id.frame_listview_active);
		lvActive.addFooterView(lvActive_footer);// 添加底部视图 必须在setAdapter前
		lvActive.setAdapter(lvActiveAdapter);
		lvActive.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// 点击头部、底部栏无效
				if (position == 0 || view == lvActive_footer)
					return;

				Active active = null;
				// 判断是否是TextView
				if (view instanceof TextView) {
					active = (Active) view.getTag();
				} else {
					TextView tv = (TextView) view
							.findViewById(R.id.active_listitem_username);
					active = (Active) tv.getTag();
				}
				if (active == null)
					return;

				// 跳转
				UIHelper.showActiveRedirect(view.getContext(), active);
			}
		});
		lvActive.setOnScrollListener(new AbsListView.OnScrollListener() {
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				lvActive.onScrollStateChanged(view, scrollState);

				// 数据为空--不用继续下面代码了
				if (lvActiveData.isEmpty())
					return;

				// 判断是否滚动到底部
				boolean scrollEnd = false;
				try {
					if (view.getPositionForView(lvActive_footer) == view
							.getLastVisiblePosition())
						scrollEnd = true;
				} catch (Exception e) {
					scrollEnd = false;
				}

				int lvDataState = StringUtils.toInt(lvActive.getTag());
				if (scrollEnd && lvDataState == UIHelper.LISTVIEW_DATA_MORE) {
					lvActive.setTag(UIHelper.LISTVIEW_DATA_LOADING);
					lvActive_foot_more.setText(R.string.load_ing);
					lvActive_foot_progress.setVisibility(View.VISIBLE);
					// 当前pageIndex
					int pageIndex = lvActiveSumData / AppContext.PAGE_SIZE;
					loadLvActiveData(curActiveCatalog, pageIndex,
							lvActiveHandler, UIHelper.LISTVIEW_ACTION_SCROLL);
				}
			}

			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				lvActive.onScroll(view, firstVisibleItem, visibleItemCount,
						totalItemCount);
			}
		});
		lvActive.setOnRefreshListener(new PullToRefreshListView.OnRefreshListener() {
			public void onRefresh() {
				// 处理通知信息
				if (curActiveCatalog == ActiveList.CATALOG_ATME
						&& bv_atme.isShown()) {
					isClearNotice = true;
					curClearNoticeType = Notice.TYPE_ATME;
				} else if (curActiveCatalog == ActiveList.CATALOG_COMMENT
						&& bv_review.isShown()) {
					isClearNotice = true;
					curClearNoticeType = Notice.TYPE_COMMENT;
				}
				// 刷新数据
				loadLvActiveData(curActiveCatalog, 0, lvActiveHandler,
						UIHelper.LISTVIEW_ACTION_REFRESH);
			}
		});
	}

	/**
	 * 初始化留言列表
	 */
	private void initMsgListView() {
		lvMsgAdapter = new ListViewMessageAdapter(this, lvMsgData,
				R.layout.message_listitem);
		lvMsg_footer = getLayoutInflater().inflate(R.layout.listview_footer,
				null);
		lvMsg_foot_more = (TextView) lvMsg_footer
				.findViewById(R.id.listview_foot_more);
		lvMsg_foot_progress = (ProgressBar) lvMsg_footer
				.findViewById(R.id.listview_foot_progress);
		lvMsg = (PullToRefreshListView) findViewById(R.id.frame_listview_message);
		lvMsg.addFooterView(lvMsg_footer);// 添加底部视图 必须在setAdapter前
		lvMsg.setAdapter(lvMsgAdapter);
		lvMsg.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// 点击头部、底部栏无效
				if (position == 0 || view == lvMsg_footer)
					return;

				Messages msg = null;
				// 判断是否是TextView
				if (view instanceof TextView) {
					msg = (Messages) view.getTag();
				} else {
					TextView tv = (TextView) view
							.findViewById(R.id.message_listitem_username);
					msg = (Messages) tv.getTag();
				}
				if (msg == null)
					return;

				// 跳转到留言详情
				UIHelper.showMessageDetail(view.getContext(),
						msg.getFriendId(), msg.getFriendName());
			}
		});
		lvMsg.setOnScrollListener(new AbsListView.OnScrollListener() {
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				lvMsg.onScrollStateChanged(view, scrollState);

				// 数据为空--不用继续下面代码了
				if (lvMsgData.isEmpty())
					return;

				// 判断是否滚动到底部
				boolean scrollEnd = false;
				try {
					if (view.getPositionForView(lvMsg_footer) == view
							.getLastVisiblePosition())
						scrollEnd = true;
				} catch (Exception e) {
					scrollEnd = false;
				}

				int lvDataState = StringUtils.toInt(lvMsg.getTag());
				if (scrollEnd && lvDataState == UIHelper.LISTVIEW_DATA_MORE) {
					lvMsg.setTag(UIHelper.LISTVIEW_DATA_LOADING);
					lvMsg_foot_more.setText(R.string.load_ing);
					lvMsg_foot_progress.setVisibility(View.VISIBLE);
					// 当前pageIndex
					int pageIndex = lvMsgSumData / AppContext.PAGE_SIZE;
					loadLvMsgData(pageIndex, lvMsgHandler,
							UIHelper.LISTVIEW_ACTION_SCROLL);
				}
			}

			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				lvMsg.onScroll(view, firstVisibleItem, visibleItemCount,
						totalItemCount);
			}
		});
		lvMsg.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				// 点击头部、底部栏无效
				if (position == 0 || view == lvMsg_footer)
					return false;

				Messages _msg = null;
				// 判断是否是TextView
				if (view instanceof TextView) {
					_msg = (Messages) view.getTag();
				} else {
					TextView tv = (TextView) view
							.findViewById(R.id.message_listitem_username);
					_msg = (Messages) tv.getTag();
				}
				if (_msg == null)
					return false;

				final Messages message = _msg;

				// 选择操作
				final Handler handler = new Handler() {
					public void handleMessage(Message msg) {
						if (msg.what == 1) {
							Result res = (Result) msg.obj;
							if (res.OK()) {
								lvMsgData.remove(message);
								lvMsgAdapter.notifyDataSetChanged();
							}
							UIHelper.ToastMessage(Main.this,
									res.getErrorMessage());
						} else {
							((AppException) msg.obj).makeToast(Main.this);
						}
					}
				};
				Thread thread = new Thread() {
					public void run() {
						Message msg = new Message();
						try {
							Result res = appContext.delMessage(
									appContext.getLoginUid(),
									message.getFriendId());
							msg.what = 1;
							msg.obj = res;
						} catch (AppException e) {
							e.printStackTrace();
							msg.what = -1;
							msg.obj = e;
						}
						handler.sendMessage(msg);
					}
				};
				UIHelper.showMessageListOptionDialog(Main.this, message, thread);
				return true;
			}
		});
		lvMsg.setOnRefreshListener(new PullToRefreshListView.OnRefreshListener() {
			public void onRefresh() {
				// 清除通知信息
				if (bv_message.isShown()) {
					isClearNotice = true;
					curClearNoticeType = Notice.TYPE_MESSAGE;
				}
				// 刷新数据
				loadLvMsgData(0, lvMsgHandler, UIHelper.LISTVIEW_ACTION_REFRESH);
			}
		});
	}

	/**
	 * 初始化头部视图
	 */
	private void initHeadView() {
		mHeadLogo = (ImageView) findViewById(R.id.main_head_logo);
		mHeadTitle = (TextView) findViewById(R.id.main_head_title);
		mHeadProgress = (ProgressBar) findViewById(R.id.main_head_progress);
		mHeadPubTask = (ImageButton) findViewById(R.id.main_head_pub_post);
		
		mHeadPubTask.setOnClickListener(new View.OnClickListener() {
		public void onClick(View v) {
			UIHelper.showTaskPub(Main.this);
			//UIHelper.showQuestionPub(v.getContext());
		}
	});
		
//		mHead_search = (ImageButton) findViewById(R.id.main_head_search);
//		mHeadPub_post = (ImageButton) findViewById(R.id.main_head_pub_post);
//		mHeadPub_tweet = (ImageButton) findViewById(R.id.main_head_pub_tweet);

//		mHead_search.setOnClickListener(new View.OnClickListener() {
//			public void onClick(View v) {
//				UIHelper.showSearch(v.getContext());
//			}
//		});
//		mHeadPub_post.setOnClickListener(new View.OnClickListener() {
//			public void onClick(View v) {
//				UIHelper.showQuestionPub(v.getContext());
//			}
//		});
//		mHeadPub_tweet.setOnClickListener(new View.OnClickListener() {
//			public void onClick(View v) {
//				UIHelper.showTweetPub(Main.this);
//			}
//		});
	}

	/**
	 * 初始化底部栏
	 * TODO: 点击问题时候切换到ToDoTask
	 */
	private void initFootBar() {
		fbNews = (RadioButton) findViewById(R.id.main_footbar_news);
		fbQuestion = (RadioButton) findViewById(R.id.main_footbar_question);
		fbTweet = (RadioButton) findViewById(R.id.main_footbar_tweet);
		fbactive = (RadioButton) findViewById(R.id.main_footbar_active);
		fbSetting = (ImageView) findViewById(R.id.main_footbar_setting);
		fbSetting.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// 展示快捷栏&判断是否登录&是否加载文章图片
				UIHelper.showSettingLoginOrLogout(Main.this,
						mGrid.getQuickAction(0));
				mGrid.show(v);
			}
		});
	}

	/**
	 * 初始化通知信息标签控件
	 * TODO: 加入消息通知数量
	 */
	private void initBadgeView() {

		bv_message = new BadgeView(this, fbTweet);
		bv_message.setBackgroundResource(R.drawable.widget_count_bg);
		bv_message.setIncludeFontPadding(false);
		bv_message.setGravity(Gravity.CENTER);
		bv_message.setTextSize(8f);
		bv_message.setTextColor(Color.WHITE);
	}

	/**
	 * 初始化水平滚动翻页
	 * TODO: 点击底部按钮切换功能
	 */
	private void initPageScroll() {
		mScrollLayout = (ScrollLayout) findViewById(R.id.main_scrolllayout);

		LinearLayout linearLayout = (LinearLayout) findViewById(R.id.main_linearlayout_footer);
		mHeadTitles = getResources().getStringArray(R.array.head_titles);
		mViewCount = mScrollLayout.getChildCount() - 3;//****因为Question被隐藏，所以减去1
		mButtons = new RadioButton[mViewCount];

		for (int i = 0; i < mViewCount; i++) {
			mButtons[i] = (RadioButton) linearLayout.getChildAt(i * 2);
			mButtons[i].setTag(i);
			mButtons[i].setChecked(false);
			mButtons[i].setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					int pos = (Integer) (v.getTag());
					// 点击当前项刷新
					if (mCurSel == pos) {
						switch (pos) {
//						case 1:
//							lvInst.clickRefresh();
//							// 资讯+博客
//							if (lvNews.getVisibility() == View.VISIBLE)
//								lvNews.clickRefresh();
//							else
//								lvBlog.clickRefresh();
//							break;
						case 0:// 待办任务
							lvTodoTask.clickRefresh();
							break;
						case 1:// 流程实例
							lvInst.clickRefresh();
							break;
						case 2:// 动弹
							lvJBNews.clickRefresh();
							//lvTweet.clickRefresh();
							break;
						case 3:// 动态+留言
							if (lvActive.getVisibility() == View.VISIBLE)
								lvActive.clickRefresh();
							else
								lvMsg.clickRefresh();
							break;
						}
					}
					mScrollLayout.snapToScreen(pos);
				}
			});
		}

		// 设置第一显示屏
		mCurSel = 0;
		mButtons[mCurSel].setChecked(true);
		
		//****设置动态暂时失效
		//mButtons[2].setEnabled(false);
		//mButtons[3].setEnabled(false);

		mScrollLayout
				.SetOnViewChangeListener(new ScrollLayout.OnViewChangeListener() {
					public void OnViewChange(int viewIndex) {
						// 切换列表视图-如果列表数据为空：加载数据
						switch (viewIndex) {
//						case 1:// 资讯
//							if (lvNews.getVisibility() == View.VISIBLE) {
//								if (lvNewsData.isEmpty()) {
//									loadLvNewsData(curNewsCatalog, 0,
//											lvNewsHandler,
//											UIHelper.LISTVIEW_ACTION_INIT);
//								}
//							} else {
//								if (lvBlogData.isEmpty()) {
//									loadLvBlogData(curNewsCatalog, 0,
//											lvBlogHandler,
//											UIHelper.LISTVIEW_ACTION_INIT);
//								}
//							}
//							break;
////						case 1:// 问答
////							if (lvQuestionData.isEmpty()) {
////								loadLvQuestionData(curQuestionCatalog, 0,
////										lvQuestionHandler,
////										UIHelper.LISTVIEW_ACTION_INIT);
////							}
////							break;
						case 0:// 待办任务
							if (!appContext.isLogin()) {
								if (lvTodoTask.getVisibility() == View.VISIBLE
										&& lvTodoTaskData.isEmpty()) {
									lvTodoTask_foot_more
											.setText(R.string.load_empty);
									lvTodoTask_foot_progress
											.setVisibility(View.GONE);
								}
								//****如果不登录，则自动退出程序
								UIHelper.showLoginDialog(Main.this);
								break;
							}
							if (lvTodoTaskData.isEmpty()) {
								loadLvTodoTaskData(curTodoCatalog, 0,
										lvTodoTaskHandler,
										UIHelper.LISTVIEW_ACTION_INIT);
							}
							break;
						case 1:// 流程实例
							if (!appContext.isLogin()) {
								if (lvInst.getVisibility() == View.VISIBLE
										&& lvInstData.isEmpty()) {
									lvInst_foot_more
											.setText(R.string.load_empty);
									lvInst_foot_progress
											.setVisibility(View.GONE);
								}
								//****如果不登录，则自动退出程序
								UIHelper.showLoginDialog(Main.this);
								break;
							}
							if (lvInstData.isEmpty()) {
								loadLvInstData(curInstCatalog, 0,
										lvInstHandler,
										UIHelper.LISTVIEW_ACTION_INIT);
							}
							break;
						case 2:// JB新闻
							if (lvJBNewsData.isEmpty()) {
								loadLvJBNewsData(curJBNewsCatalog, 0,
										lvJBNewsHandler,
										UIHelper.LISTVIEW_ACTION_INIT);
							}
							break;
						case 3:// 动态
								// 判断登录
							if (!appContext.isLogin()) {
								if (lvActive.getVisibility() == View.VISIBLE
										&& lvActiveData.isEmpty()) {
									lvActive_foot_more
											.setText(R.string.load_empty);
									lvActive_foot_progress
											.setVisibility(View.GONE);
								} else if (lvMsg.getVisibility() == View.VISIBLE
										&& lvMsgData.isEmpty()) {
									lvMsg_foot_more
											.setText(R.string.load_empty);
									lvMsg_foot_progress
											.setVisibility(View.GONE);
								}
								UIHelper.showLoginDialog(Main.this);
								break;
							}
							// 处理通知信息
							if (bv_atme.isShown())
								frameActiveBtnOnClick(framebtn_Active_atme,
										ActiveList.CATALOG_ATME,
										UIHelper.LISTVIEW_ACTION_REFRESH);
							else if (bv_review.isShown())
								frameActiveBtnOnClick(framebtn_Active_comment,
										ActiveList.CATALOG_COMMENT,
										UIHelper.LISTVIEW_ACTION_REFRESH);
							else if (bv_message.isShown())
								frameActiveBtnOnClick(framebtn_Active_message,
										0, UIHelper.LISTVIEW_ACTION_REFRESH);
							else if (lvActive.getVisibility() == View.VISIBLE
									&& lvActiveData.isEmpty())
								loadLvActiveData(curActiveCatalog, 0,
										lvActiveHandler,
										UIHelper.LISTVIEW_ACTION_INIT);
							else if (lvMsg.getVisibility() == View.VISIBLE
									&& lvMsgData.isEmpty())
								loadLvMsgData(0, lvMsgHandler,
										UIHelper.LISTVIEW_ACTION_INIT);
							break;
						}
						setCurPoint(viewIndex);
					}
				});
	}

	/**
	 * 设置底部栏当前焦点
	 * TODO: 改变显示列表后的顶部图标和标题****
	 * 
	 * @param index
	 */
	private void setCurPoint(int index) {
		if (index < 0 || index > mViewCount || mCurSel == index)
			return;

		mButtons[mCurSel].setChecked(false);
		mButtons[index].setChecked(true);
		mHeadTitle.setText(mHeadTitles[index]);
		mCurSel = index;

		mHeadPubTask.setVisibility(View.GONE);
		//mHead_search.setVisibility(View.GONE);
		//mHeadPub_post.setVisibility(View.GONE);
		//mHeadPub_tweet.setVisibility(View.GONE);
		// 头部logo、发帖、发动弹按钮显示
		if (index == 0) {//我的任务
			mHeadLogo.setImageResource(R.drawable.frame_logo_tweet);
			
			//mHead_search.setVisibility(View.VISIBLE);
		} else if (index == 1) {//当前工作流
			mHeadLogo.setImageResource(R.drawable.frame_logo_news);
			mHeadPubTask.setVisibility(View.VISIBLE);
			//mHeadPub_post.setVisibility(View.VISIBLE);
		} else if (index == 2) {
			mHeadLogo.setImageResource(R.drawable.frame_logo_tweet);
			//mHeadPub_tweet.setVisibility(View.VISIBLE);
		} else if (index == 3) {
			mHeadLogo.setImageResource(R.drawable.frame_logo_active);
			//mHeadPub_tweet.setVisibility(View.VISIBLE);
		}
	}

	/**
	 * 初始化各个主页的按钮(资讯、问答、动弹、动态、留言)
	 */
	private void initFrameButton() {
		// 初始化按钮控件
		
		framebtn_Inst_running = (Button) findViewById(R.id.frame_btn_processinst_running);
		framebtn_Inst_complete = (Button) findViewById(R.id.frame_btn_processinst_complete);
		
		framebtn_TodoTask_claim = (Button) findViewById(R.id.frame_btn_todotask_claim);
		framebtn_TodoTask_todo = (Button) findViewById(R.id.frame_btn_todotask_todo);
		framebtn_TodoTask_all = (Button) findViewById(R.id.frame_btn_todotask_all);
		
		framebtn_News_lastest = (Button) findViewById(R.id.frame_btn_news_lastest);
		framebtn_News_blog = (Button) findViewById(R.id.frame_btn_news_blog);
		framebtn_News_recommend = (Button) findViewById(R.id.frame_btn_news_recommend);
		framebtn_Question_ask = (Button) findViewById(R.id.frame_btn_question_ask);
		framebtn_Question_share = (Button) findViewById(R.id.frame_btn_question_share);
		framebtn_Question_other = (Button) findViewById(R.id.frame_btn_question_other);
		framebtn_Question_job = (Button) findViewById(R.id.frame_btn_question_job);
		framebtn_Question_site = (Button) findViewById(R.id.frame_btn_question_site);
		framebtn_Tweet_lastest = (Button) findViewById(R.id.frame_btn_tweet_lastest);
		framebtn_Tweet_hot = (Button) findViewById(R.id.frame_btn_tweet_hot);
		framebtn_Tweet_my = (Button) findViewById(R.id.frame_btn_tweet_my);
		framebtn_Active_lastest = (Button) findViewById(R.id.frame_btn_active_lastest);
		framebtn_Active_atme = (Button) findViewById(R.id.frame_btn_active_atme);
		framebtn_Active_comment = (Button) findViewById(R.id.frame_btn_active_comment);
		framebtn_Active_myself = (Button) findViewById(R.id.frame_btn_active_myself);
		framebtn_Active_message = (Button) findViewById(R.id.frame_btn_active_message);
		
		// 设置首选择项
		framebtn_Inst_running.setEnabled(false);
		framebtn_TodoTask_claim.setEnabled(false);
		framebtn_News_lastest.setEnabled(false);
		framebtn_Question_ask.setEnabled(false);
		framebtn_Tweet_lastest.setEnabled(false);
		framebtn_Active_lastest.setEnabled(false);
		
		//流程实例
		framebtn_Inst_running.setOnClickListener(frameInstBtnClick(
				framebtn_Inst_running, InstanceList.CATALOG_RUNNING));
		framebtn_Inst_complete.setOnClickListener(frameInstBtnClick(
				framebtn_Inst_complete, InstanceList.CATALOG_COMPLETE));
		//待办任务
		framebtn_TodoTask_claim.setOnClickListener(frameTodoTaskBtnClick(
				framebtn_TodoTask_claim, TodoTask.CATALOG_CLAIM));
		framebtn_TodoTask_todo.setOnClickListener(frameTodoTaskBtnClick(
				framebtn_TodoTask_todo, TodoTask.CATALOG_TODO));
		framebtn_TodoTask_all.setOnClickListener(frameTodoTaskBtnClick(
				framebtn_TodoTask_all, TodoTask.CATALOG_ALL));
		// 资讯+博客
		framebtn_News_lastest.setOnClickListener(frameNewsBtnClick(
				framebtn_News_lastest, NewsList.CATALOG_ALL));
		framebtn_News_blog.setOnClickListener(frameNewsBtnClick(
				framebtn_News_blog, BlogList.CATALOG_LATEST));
		framebtn_News_recommend.setOnClickListener(frameNewsBtnClick(
				framebtn_News_recommend, BlogList.CATALOG_RECOMMEND));
		// 问答
		framebtn_Question_ask.setOnClickListener(frameQuestionBtnClick(
				framebtn_Question_ask, PostList.CATALOG_ASK));
		framebtn_Question_share.setOnClickListener(frameQuestionBtnClick(
				framebtn_Question_share, PostList.CATALOG_SHARE));
		framebtn_Question_other.setOnClickListener(frameQuestionBtnClick(
				framebtn_Question_other, PostList.CATALOG_OTHER));
		framebtn_Question_job.setOnClickListener(frameQuestionBtnClick(
				framebtn_Question_job, PostList.CATALOG_JOB));
		framebtn_Question_site.setOnClickListener(frameQuestionBtnClick(
				framebtn_Question_site, PostList.CATALOG_SITE));
		// 动弹
		framebtn_Tweet_lastest.setOnClickListener(frameTweetBtnClick(
				framebtn_Tweet_lastest, TweetList.CATALOG_LASTEST));
		framebtn_Tweet_hot.setOnClickListener(frameTweetBtnClick(
				framebtn_Tweet_hot, TweetList.CATALOG_HOT));
		framebtn_Tweet_my.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// 判断登录
				int uid = appContext.getLoginUid();
				if (uid == 0) {
					UIHelper.showLoginDialog(Main.this);
					return;
				}

				framebtn_Tweet_lastest.setEnabled(true);
				framebtn_Tweet_hot.setEnabled(true);
				framebtn_Tweet_my.setEnabled(false);

				curTweetCatalog = uid;
				loadLvTweetData(curTweetCatalog, 0, lvTweetHandler,
						UIHelper.LISTVIEW_ACTION_CHANGE_CATALOG);
			}
		});
		// 动态+留言
		framebtn_Active_lastest.setOnClickListener(frameActiveBtnClick(
				framebtn_Active_lastest, ActiveList.CATALOG_LASTEST));
		framebtn_Active_atme.setOnClickListener(frameActiveBtnClick(
				framebtn_Active_atme, ActiveList.CATALOG_ATME));
		framebtn_Active_comment.setOnClickListener(frameActiveBtnClick(
				framebtn_Active_comment, ActiveList.CATALOG_COMMENT));
		framebtn_Active_myself.setOnClickListener(frameActiveBtnClick(
				framebtn_Active_myself, ActiveList.CATALOG_MYSELF));
		framebtn_Active_message.setOnClickListener(frameActiveBtnClick(
				framebtn_Active_message, 0));
		// 特殊处理
		framebtn_Active_atme.setText("@"
				+ getString(R.string.frame_title_active_atme));
	}
	
	private View.OnClickListener frameInstBtnClick(final Button btn,
			final int catalog) {
		return new View.OnClickListener() {
			public void onClick(View v) {
				// 判断登录
				if (!appContext.isLogin()) {
					if (lvTodoTask.getVisibility() == View.VISIBLE
							&& lvTodoTaskData.isEmpty()) {
						lvTodoTask_foot_more
								.setText(R.string.load_empty);
						lvTodoTask_foot_progress
								.setVisibility(View.GONE);
					}
					//****如果不登录，则自动退出程序
					UIHelper.showLoginDialog(Main.this);
					return;
				}
				
				if (btn == framebtn_Inst_running)
					framebtn_Inst_running.setEnabled(false);
				else
					framebtn_Inst_running.setEnabled(true);
				if (btn == framebtn_Inst_complete)
					framebtn_Inst_complete.setEnabled(false);
				else
					framebtn_Inst_complete.setEnabled(true);

				curInstCatalog = catalog;
				loadLvInstData(curInstCatalog, 0, lvInstHandler,
						UIHelper.LISTVIEW_ACTION_CHANGE_CATALOG);
			}
		};
	}
	
	private View.OnClickListener frameTodoTaskBtnClick(final Button btn,
			final int catalog) {
		return new View.OnClickListener() {
			public void onClick(View v) {
				// 判断登录
				if (!appContext.isLogin()) {
					if (lvTodoTask.getVisibility() == View.VISIBLE
							&& lvTodoTaskData.isEmpty()) {
						lvTodoTask_foot_more
								.setText(R.string.load_empty);
						lvTodoTask_foot_progress
								.setVisibility(View.GONE);
					}
					//****如果不登录，则自动退出程序
					UIHelper.showLoginDialog(Main.this);
					return;
				}
				
				if (btn == framebtn_TodoTask_claim)
					framebtn_TodoTask_claim.setEnabled(false);
				else
					framebtn_TodoTask_claim.setEnabled(true);
				if (btn == framebtn_TodoTask_todo)
					framebtn_TodoTask_todo.setEnabled(false);
				else
					framebtn_TodoTask_todo.setEnabled(true);
				if (btn == framebtn_TodoTask_all)
					framebtn_TodoTask_all.setEnabled(false);
				else
					framebtn_TodoTask_all.setEnabled(true);

				curTodoCatalog = catalog;
				loadLvTodoTaskData(curTodoCatalog, 0, lvTodoTaskHandler,
						UIHelper.LISTVIEW_ACTION_CHANGE_CATALOG);
			}
		};
	}

	private View.OnClickListener frameNewsBtnClick(final Button btn,
			final int catalog) {
		return new View.OnClickListener() {
			public void onClick(View v) {
				if (btn == framebtn_News_lastest) {
					framebtn_News_lastest.setEnabled(false);
				} else {
					framebtn_News_lastest.setEnabled(true);
				}
				if (btn == framebtn_News_blog) {
					framebtn_News_blog.setEnabled(false);
				} else {
					framebtn_News_blog.setEnabled(true);
				}
				if (btn == framebtn_News_recommend) {
					framebtn_News_recommend.setEnabled(false);
				} else {
					framebtn_News_recommend.setEnabled(true);
				}

				curNewsCatalog = catalog;

				// 非新闻列表
				if (btn == framebtn_News_lastest) {
					lvNews.setVisibility(View.VISIBLE);
					lvBlog.setVisibility(View.GONE);

					loadLvNewsData(curNewsCatalog, 0, lvNewsHandler,
							UIHelper.LISTVIEW_ACTION_CHANGE_CATALOG);
				} else {
					lvNews.setVisibility(View.GONE);
					lvBlog.setVisibility(View.VISIBLE);

					loadLvBlogData(curNewsCatalog, 0, lvBlogHandler,
							UIHelper.LISTVIEW_ACTION_CHANGE_CATALOG);
				}
			}
		};
	}

	private View.OnClickListener frameQuestionBtnClick(final Button btn,
			final int catalog) {
		return new View.OnClickListener() {
			public void onClick(View v) {
				if (btn == framebtn_Question_ask)
					framebtn_Question_ask.setEnabled(false);
				else
					framebtn_Question_ask.setEnabled(true);
				if (btn == framebtn_Question_share)
					framebtn_Question_share.setEnabled(false);
				else
					framebtn_Question_share.setEnabled(true);
				if (btn == framebtn_Question_other)
					framebtn_Question_other.setEnabled(false);
				else
					framebtn_Question_other.setEnabled(true);
				if (btn == framebtn_Question_job)
					framebtn_Question_job.setEnabled(false);
				else
					framebtn_Question_job.setEnabled(true);
				if (btn == framebtn_Question_site)
					framebtn_Question_site.setEnabled(false);
				else
					framebtn_Question_site.setEnabled(true);

				curQuestionCatalog = catalog;
				loadLvQuestionData(curQuestionCatalog, 0, lvQuestionHandler,
						UIHelper.LISTVIEW_ACTION_CHANGE_CATALOG);
			}
		};
	}

	private View.OnClickListener frameTweetBtnClick(final Button btn,
			final int catalog) {
		return new View.OnClickListener() {
			public void onClick(View v) {
				if (btn == framebtn_Tweet_lastest)
					framebtn_Tweet_lastest.setEnabled(false);
				else
					framebtn_Tweet_lastest.setEnabled(true);
				if (btn == framebtn_Tweet_hot)
					framebtn_Tweet_hot.setEnabled(false);
				else
					framebtn_Tweet_hot.setEnabled(true);
				if (btn == framebtn_Tweet_my)
					framebtn_Tweet_my.setEnabled(false);
				else
					framebtn_Tweet_my.setEnabled(true);

				curTweetCatalog = catalog;
				loadLvTweetData(curTweetCatalog, 0, lvTweetHandler,
						UIHelper.LISTVIEW_ACTION_CHANGE_CATALOG);
			}
		};
	}

	private View.OnClickListener frameActiveBtnClick(final Button btn,
			final int catalog) {
		return new View.OnClickListener() {
			public void onClick(View v) {
				// 判断登录
				if (!appContext.isLogin()) {
					if (lvActive.getVisibility() == View.VISIBLE
							&& lvActiveData.isEmpty()) {
						lvActive_foot_more.setText(R.string.load_empty);
						lvActive_foot_progress.setVisibility(View.GONE);
					} else if (lvMsg.getVisibility() == View.VISIBLE
							&& lvMsgData.isEmpty()) {
						lvMsg_foot_more.setText(R.string.load_empty);
						lvMsg_foot_progress.setVisibility(View.GONE);
					}
					UIHelper.showLoginDialog(Main.this);
					return;
				}

				frameActiveBtnOnClick(btn, catalog,
						UIHelper.LISTVIEW_ACTION_CHANGE_CATALOG);
			}
		};
	}

	private void frameActiveBtnOnClick(Button btn, int catalog, int action) {
		if (btn == framebtn_Active_lastest)
			framebtn_Active_lastest.setEnabled(false);
		else
			framebtn_Active_lastest.setEnabled(true);
		if (btn == framebtn_Active_atme)
			framebtn_Active_atme.setEnabled(false);
		else
			framebtn_Active_atme.setEnabled(true);
		if (btn == framebtn_Active_comment)
			framebtn_Active_comment.setEnabled(false);
		else
			framebtn_Active_comment.setEnabled(true);
		if (btn == framebtn_Active_myself)
			framebtn_Active_myself.setEnabled(false);
		else
			framebtn_Active_myself.setEnabled(true);
		if (btn == framebtn_Active_message)
			framebtn_Active_message.setEnabled(false);
		else
			framebtn_Active_message.setEnabled(true);

		// 是否处理通知信息
		if (btn == framebtn_Active_atme && bv_atme.isShown()) {
			action = UIHelper.LISTVIEW_ACTION_REFRESH;
			this.isClearNotice = true;
			this.curClearNoticeType = Notice.TYPE_ATME;
		} else if (btn == framebtn_Active_comment && bv_review.isShown()) {
			action = UIHelper.LISTVIEW_ACTION_REFRESH;
			this.isClearNotice = true;
			this.curClearNoticeType = Notice.TYPE_COMMENT;
		} else if (btn == framebtn_Active_message && bv_message.isShown()) {
			action = UIHelper.LISTVIEW_ACTION_REFRESH;
			this.isClearNotice = true;
			this.curClearNoticeType = Notice.TYPE_MESSAGE;
		}

		// 非留言展示动态列表
		if (btn != framebtn_Active_message) {
			lvActive.setVisibility(View.VISIBLE);
			lvMsg.setVisibility(View.GONE);

			curActiveCatalog = catalog;
			loadLvActiveData(curActiveCatalog, 0, lvActiveHandler, action);
		} else {
			lvActive.setVisibility(View.GONE);
			lvMsg.setVisibility(View.VISIBLE);

			loadLvMsgData(0, lvMsgHandler, action);
		}
	}

	/**
	 * 获取listview的初始化Handler
	 * TODO: 待办任务列表是否允许翻页？JB新闻目前暂时不支持翻页功能，需要处理
	 * @param lv
	 * @param adapter
	 * @return
	 */
	private Handler getLvHandler(final PullToRefreshListView lv,
			final BaseAdapter adapter, final TextView more,
			final ProgressBar progress, final int pageSize) {
		return new Handler() {
			public void handleMessage(Message msg) {
				if (msg.what >= 0) {
					// listview数据处理，主要处理是否有新数据到来，将旧数据和新数据来合并
					Notice notice = handleLvData(msg.what, msg.obj, msg.arg2,
							msg.arg1);

					if (msg.what < pageSize) {//如果返回的pageSize小于20，则表明读取了全部数据，不需要翻页
						lv.setTag(UIHelper.LISTVIEW_DATA_FULL);
						adapter.notifyDataSetChanged();
						more.setText(R.string.load_full);
					} else if (msg.what == pageSize) {
						lv.setTag(UIHelper.LISTVIEW_DATA_MORE);
						adapter.notifyDataSetChanged();
						more.setText(R.string.load_more);

						// 特殊处理-热门动弹不能翻页
						if (lv == lvTweet) {
							TweetList tlist = (TweetList) msg.obj;
							if (lvTweetData.size() == tlist.getTweetCount()) {
								lv.setTag(UIHelper.LISTVIEW_DATA_FULL);
								more.setText(R.string.load_full);
							}
						}
					}
					// 发送通知广播
					if (notice != null) {
						UIHelper.sendBroadCast(lv.getContext(), notice);
					}
					// 是否清除通知信息
					if (isClearNotice) {
						ClearNotice(curClearNoticeType);
						isClearNotice = false;// 重置
						curClearNoticeType = 0;
					}
				} else if (msg.what == -1) {
					// 有异常--显示加载出错 & 弹出错误消息
					lv.setTag(UIHelper.LISTVIEW_DATA_MORE);
					more.setText(R.string.load_error);
					((AppException) msg.obj).makeToast(Main.this);
				}
				if (adapter.getCount() == 0) {
					lv.setTag(UIHelper.LISTVIEW_DATA_EMPTY);
					more.setText(R.string.load_empty);
				}
				progress.setVisibility(ProgressBar.GONE);
				mHeadProgress.setVisibility(ProgressBar.GONE);
				if (msg.arg1 == UIHelper.LISTVIEW_ACTION_REFRESH || msg.arg1 == UIHelper.LISTVIEW_ACTION_AUTO_REFRESH) {
					lv.onRefreshComplete(getString(R.string.pull_to_refresh_update)
							+ new Date().toLocaleString());
					lv.setSelection(0);
				} else if (msg.arg1 == UIHelper.LISTVIEW_ACTION_CHANGE_CATALOG) {
					lv.onRefreshComplete();
					lv.setSelection(0);
				}
			}
		};
	}

	/**
	 * listview数据处理
	 * TODO: 新增todo类型的处理
	 * 
	 * @param what
	 *            数量
	 * @param obj
	 *            数据
	 * @param objtype
	 *            数据类型
	 * @param actiontype
	 *            操作类型
	 * @return notice 通知信息
	 */
	private Notice handleLvData(int what, Object obj, int objtype,
			int actiontype) {
		Notice notice = null;
		switch (actiontype) {
		case UIHelper.LISTVIEW_ACTION_INIT:
		case UIHelper.LISTVIEW_ACTION_REFRESH:
		case UIHelper.LISTVIEW_ACTION_AUTO_REFRESH:
		case UIHelper.LISTVIEW_ACTION_CHANGE_CATALOG:
			int newdata = 0;// 新加载数据-只有刷新动作才会使用到
			switch (objtype) {
			case UIHelper.LISTVIEW_DATATYPE_NEWS:
				NewsList nlist = (NewsList) obj;
				notice = nlist.getNotice();
				//**** for test 测试notification
				notice = new Notice();
				notice.setMsgCount(10);
				lvNewsSumData = what;
				if (actiontype == UIHelper.LISTVIEW_ACTION_REFRESH) {
					if (lvNewsData.size() > 0) {
						for (News news1 : nlist.getNewslist()) {
							boolean b = false;
							for (News news2 : lvNewsData) {
								if (news1.getId() == news2.getId()) {
									b = true;
									break;
								}
							}
							if (!b)
								newdata++;
						}
					} else {
						newdata = what;
					}
				}
				lvNewsData.clear();// 先清除原有数据
				lvNewsData.addAll(nlist.getNewslist());
				break;
			case UIHelper.LISTVIEW_DATATYPE_BLOG:
				BlogList blist = (BlogList) obj;
				notice = blist.getNotice();
				lvBlogSumData = what;
				if (actiontype == UIHelper.LISTVIEW_ACTION_REFRESH) {
					if (lvBlogData.size() > 0) {
						for (Blog blog1 : blist.getBloglist()) {
							boolean b = false;
							for (Blog blog2 : lvBlogData) {
								if (blog1.getId() == blog2.getId()) {
									b = true;
									break;
								}
							}
							if (!b)
								newdata++;
						}
					} else {
						newdata = what;
					}
				}
				lvBlogData.clear();// 先清除原有数据
				lvBlogData.addAll(blist.getBloglist());
				break;
				
			case UIHelper.LISTVIEW_DATATYPE_JBNEWS:
				JBNewsList jbNewsList = (JBNewsList) obj;
				notice = jbNewsList.getNotice();
				lvJBNewsSumData = what;//scroll时候是lvTodoSumData += what
				if (actiontype == UIHelper.LISTVIEW_ACTION_REFRESH) {
					if (lvInstData.size() > 0) {
						for (JBNews inst1 : jbNewsList.getJBNewslist()) {
							boolean b = false;
							for (JBNews inst2 : lvJBNewsData) {
								if (inst1.getId() == inst2.getId()) {
									b = true;
									break;
								}
							}
							if (!b)
								newdata++;
						}
					} else {
						newdata = what;
					}
				}
				lvJBNewsData.clear();// 先清除原有数据
				lvJBNewsData.addAll(jbNewsList.getJBNewslist());
				break;
				
				//流程实例
			case UIHelper.LISTVIEW_DATATYPE_INST:
				InstanceList instlist = (InstanceList) obj;
				notice = instlist.getNotice();
				lvInstSumData = what;//scroll时候是lvTodoSumData += what
				if (actiontype == UIHelper.LISTVIEW_ACTION_REFRESH) {
					if (lvInstData.size() > 0) {
						for (ProcessInst inst1 : instlist.getInslist()) {
							boolean b = false;
							for (ProcessInst inst2 : lvInstData) {
								if (inst1.getId() == inst2.getId()) {
									b = true;
									break;
								}
							}
							if (!b)
								newdata++;
						}
					} else {
						newdata = what;
					}
				}
				lvInstData.clear();// 先清除原有数据
				lvInstData.addAll(instlist.getInslist());
				break;
				//****处理得到的数据，刷新时候得到新数据的数量，另外，处理完数据还会显示1条？****
			case UIHelper.LISTVIEW_DATATYPE_TODO:
				TodoList taskList = (TodoList) obj;
				notice = taskList.getNotice();
				lvTodoSumData = what;//scroll时候是lvTodoSumData += what
				if (actiontype == UIHelper.LISTVIEW_ACTION_REFRESH 
						|| actiontype == UIHelper.LISTVIEW_ACTION_AUTO_REFRESH) {
					if (lvTodoTaskData.size() > 0) {
						for (TodoTask task1 : taskList.getTodolist()) {
							boolean b = false;
							for (TodoTask task2 : lvTodoTaskData) {
								if (task1.getId() == task2.getId()) {
									b = true;
									break;
								}
							}
							if (!b)
								newdata++;
						}
					} else {
						newdata = what;
					}
				}
				if (actiontype == UIHelper.LISTVIEW_ACTION_REFRESH && curTodoCatalog == TodoTask.CATALOG_TODO) {
					if(newdata > 0) {
						bv_message.setText(newdata+"");
						bv_message.show();
					}else{
						Main.bv_message.setText("");
						Main.bv_message.hide();
					}
				}
				
				lvTodoTaskData.clear();// 先清除原有数据
				lvTodoTaskData.addAll(taskList.getTodolist());
				break;
			case UIHelper.LISTVIEW_DATATYPE_POST:
				PostList plist = (PostList) obj;
				notice = plist.getNotice();
				lvQuestionSumData = what;
				if (actiontype == UIHelper.LISTVIEW_ACTION_REFRESH) {
					if (lvQuestionData.size() > 0) {
						for (Post post1 : plist.getPostlist()) {
							boolean b = false;
							for (Post post2 : lvQuestionData) {
								if (post1.getId() == post2.getId()) {
									b = true;
									break;
								}
							}
							if (!b)
								newdata++;
						}
					} else {
						newdata = what;
					}
				}
				lvQuestionData.clear();// 先清除原有数据
				lvQuestionData.addAll(plist.getPostlist());
				break;
			case UIHelper.LISTVIEW_DATATYPE_TWEET:
				TweetList tlist = (TweetList) obj;
				notice = tlist.getNotice();
				lvTweetSumData = what;
				if (actiontype == UIHelper.LISTVIEW_ACTION_REFRESH) {
					if (lvTweetData.size() > 0) {
						for (Tweet tweet1 : tlist.getTweetlist()) {
							boolean b = false;
							for (Tweet tweet2 : lvTweetData) {
								if (tweet1.getId() == tweet2.getId()) {
									b = true;
									break;
								}
							}
							if (!b)
								newdata++;
						}
					} else {
						newdata = what;
					}
				}
				lvTweetData.clear();// 先清除原有数据
				lvTweetData.addAll(tlist.getTweetlist());
				break;
			case UIHelper.LISTVIEW_DATATYPE_ACTIVE:
				ActiveList alist = (ActiveList) obj;
				notice = alist.getNotice();
				lvActiveSumData = what;
				if (actiontype == UIHelper.LISTVIEW_ACTION_REFRESH) {
					if (lvActiveData.size() > 0) {
						for (Active active1 : alist.getActivelist()) {
							boolean b = false;
							for (Active active2 : lvActiveData) {
								if (active1.getId() == active2.getId()) {
									b = true;
									break;
								}
							}
							if (!b)
								newdata++;
						}
					} else {
						newdata = what;
					}
				}
				lvActiveData.clear();// 先清除原有数据
				lvActiveData.addAll(alist.getActivelist());
				break;
			case UIHelper.LISTVIEW_DATATYPE_MESSAGE:
				MessageList mlist = (MessageList) obj;
				notice = mlist.getNotice();
				lvMsgSumData = what;
				if (actiontype == UIHelper.LISTVIEW_ACTION_REFRESH) {
					if (lvMsgData.size() > 0) {
						for (Messages msg1 : mlist.getMessagelist()) {
							boolean b = false;
							for (Messages msg2 : lvMsgData) {
								if (msg1.getId() == msg2.getId()) {
									b = true;
									break;
								}
							}
							if (!b)
								newdata++;
						}
					} else {
						newdata = what;
					}
				}
				lvMsgData.clear();// 先清除原有数据
				lvMsgData.addAll(mlist.getMessagelist());
				break;
			}
			if (actiontype == UIHelper.LISTVIEW_ACTION_REFRESH 
					|| actiontype == UIHelper.LISTVIEW_ACTION_AUTO_REFRESH) {
				// 提示新加载数据****刷新有数据时候还会有声音
				if (newdata > 0) {
					NewDataToast
							.makeText(
									this,
									getString(R.string.new_data_toast_message,
											newdata), appContext.isAppSound())
							.show();
				} else {
					//没有新数据时候不弹出toast框
//					NewDataToast.makeText(this,
//							getString(R.string.new_data_toast_none), false)
//							.show();
				}
			}
			//newdata > 0 && ****
			if (actiontype == UIHelper.LISTVIEW_ACTION_AUTO_REFRESH) {
				Notice todoNotice = new Notice();
				todoNotice.setTaskCount(newdata);
				notice = todoNotice;
			}
			break;
		case UIHelper.LISTVIEW_ACTION_SCROLL:
			switch (objtype) {
			case UIHelper.LISTVIEW_DATATYPE_NEWS:
				NewsList list = (NewsList) obj;
				notice = list.getNotice();
				lvNewsSumData += what;
				if (lvNewsData.size() > 0) {
					for (News news1 : list.getNewslist()) {
						boolean b = false;
						for (News news2 : lvNewsData) {
							if (news1.getId() == news2.getId()) {
								b = true;
								break;
							}
						}
						if (!b)
							lvNewsData.add(news1);
					}
				} else {
					lvNewsData.addAll(list.getNewslist());
				}
				break;
			case UIHelper.LISTVIEW_DATATYPE_BLOG:
				BlogList blist = (BlogList) obj;
				notice = blist.getNotice();
				lvBlogSumData += what;
				if (lvBlogData.size() > 0) {
					for (Blog blog1 : blist.getBloglist()) {
						boolean b = false;
						for (Blog blog2 : lvBlogData) {
							if (blog1.getId() == blog2.getId()) {
								b = true;
								break;
							}
						}
						if (!b)
							lvBlogData.add(blog1);
					}
				} else {
					lvBlogData.addAll(blist.getBloglist());
				}
				break;
			case UIHelper.LISTVIEW_DATATYPE_POST:
				PostList plist = (PostList) obj;
				notice = plist.getNotice();
				lvQuestionSumData += what;
				if (lvQuestionData.size() > 0) {
					for (Post post1 : plist.getPostlist()) {
						boolean b = false;
						for (Post post2 : lvQuestionData) {
							if (post1.getId() == post2.getId()) {
								b = true;
								break;
							}
						}
						if (!b)
							lvQuestionData.add(post1);
					}
				} else {
					lvQuestionData.addAll(plist.getPostlist());
				}
				break;
				
			case UIHelper.LISTVIEW_DATATYPE_JBNEWS:
				JBNewsList jbNewsList = (JBNewsList) obj;
				notice = jbNewsList.getNotice();
				lvJBNewsSumData += what;//scroll时候是lvTodoSumData += what
				if (lvInstData.size() > 0) {
					for (JBNews inst1 : jbNewsList.getJBNewslist()) {
						boolean b = false;
						for (JBNews inst2 : lvJBNewsData) {
							if (inst1.getId() == inst2.getId()) {
								b = true;
								break;
							}
						}
						if (!b)
							lvJBNewsData.add(inst1);
					}
				} else {
					lvJBNewsData.addAll(jbNewsList.getJBNewslist());
				}
				break;
				
				//流程实例
			case UIHelper.LISTVIEW_DATATYPE_INST:
				InstanceList instlist = (InstanceList) obj;
				notice = instlist.getNotice();
				lvInstSumData += what;
				if (lvInstData.size() > 0) {
					for (ProcessInst inst1 : instlist.getInslist()) {
						boolean b = false;
						for (ProcessInst task2 : lvInstData) {
							if (inst1.getId() == task2.getId()) {
								b = true;
								break;
							}
						}
						if (!b)
							lvInstData.add(inst1);
					}
				} else {
					lvInstData.addAll(instlist.getInslist());
				}
				break;
				//for wf 新增待办任务的新数据处理
			case UIHelper.LISTVIEW_DATATYPE_TODO:
				TodoList taskList = (TodoList) obj;
				notice = taskList.getNotice();
				lvTodoSumData += what;
				if (lvTodoTaskData.size() > 0) {
					for (TodoTask task1 : taskList.getTodolist()) {
						boolean b = false;
						for (TodoTask task2 : lvTodoTaskData) {
							if (task1.getId() == task2.getId()) {
								b = true;
								break;
							}
						}
						if (!b)
							lvTodoTaskData.add(task1);
					}
				} else {
					lvTodoTaskData.addAll(taskList.getTodolist());
				}
				break;
			case UIHelper.LISTVIEW_DATATYPE_TWEET:
				TweetList tlist = (TweetList) obj;
				notice = tlist.getNotice();
				lvTweetSumData += what;
				if (lvTweetData.size() > 0) {
					for (Tweet tweet1 : tlist.getTweetlist()) {
						boolean b = false;
						for (Tweet tweet2 : lvTweetData) {
							if (tweet1.getId() == tweet2.getId()) {
								b = true;
								break;
							}
						}
						if (!b)
							lvTweetData.add(tweet1);
					}
				} else {
					lvTweetData.addAll(tlist.getTweetlist());
				}
				break;
			case UIHelper.LISTVIEW_DATATYPE_ACTIVE:
				ActiveList alist = (ActiveList) obj;
				notice = alist.getNotice();
				lvActiveSumData += what;
				if (lvActiveData.size() > 0) {
					for (Active active1 : alist.getActivelist()) {
						boolean b = false;
						for (Active active2 : lvActiveData) {
							if (active1.getId() == active2.getId()) {
								b = true;
								break;
							}
						}
						if (!b)
							lvActiveData.add(active1);
					}
				} else {
					lvActiveData.addAll(alist.getActivelist());
				}
				break;
			case UIHelper.LISTVIEW_DATATYPE_MESSAGE:
				MessageList mlist = (MessageList) obj;
				notice = mlist.getNotice();
				lvMsgSumData += what;
				if (lvMsgData.size() > 0) {
					for (Messages msg1 : mlist.getMessagelist()) {
						boolean b = false;
						for (Messages msg2 : lvMsgData) {
							if (msg1.getId() == msg2.getId()) {
								b = true;
								break;
							}
						}
						if (!b)
							lvMsgData.add(msg1);
					}
				} else {
					lvMsgData.addAll(mlist.getMessagelist());
				}
				break;
			}
			break;
		}
		return notice;
	}

	/**
	 * 线程加载新闻数据
	 * 
	 * @param catalog
	 *            分类
	 * @param pageIndex
	 *            当前页数
	 * @param handler
	 *            处理器
	 * @param action
	 *            动作标识
	 */
	private void loadLvNewsData(final int catalog, final int pageIndex,
			final Handler handler, final int action) {
		mHeadProgress.setVisibility(ProgressBar.VISIBLE);
		new Thread() {
			public void run() {
				Message msg = new Message();
				boolean isRefresh = false;
				if (action == UIHelper.LISTVIEW_ACTION_REFRESH
						|| action == UIHelper.LISTVIEW_ACTION_SCROLL)
					isRefresh = true;
				try {
					NewsList list = appContext.getNewsList(catalog, pageIndex,
							isRefresh);
					msg.what = list.getPageSize();
					msg.obj = list;
				} catch (AppException e) {
					e.printStackTrace();
					msg.what = -1;
					msg.obj = e;
				}
				msg.arg1 = action;
				msg.arg2 = UIHelper.LISTVIEW_DATATYPE_NEWS;
				if (curNewsCatalog == catalog)
					handler.sendMessage(msg);
			}
		}.start();
	}

	/**
	 * 线程加载博客数据
	 * 
	 * @param catalog
	 *            分类
	 * @param pageIndex
	 *            当前页数
	 * @param handler
	 *            处理器
	 * @param action
	 *            动作标识
	 */
	private void loadLvBlogData(final int catalog, final int pageIndex,
			final Handler handler, final int action) {
		mHeadProgress.setVisibility(ProgressBar.VISIBLE);
		new Thread() {
			public void run() {
				Message msg = new Message();
				boolean isRefresh = false;
				if (action == UIHelper.LISTVIEW_ACTION_REFRESH
						|| action == UIHelper.LISTVIEW_ACTION_SCROLL)
					isRefresh = true;
				String type = "";
				switch (catalog) {
				case BlogList.CATALOG_LATEST:
					type = BlogList.TYPE_LATEST;
					break;
				case BlogList.CATALOG_RECOMMEND:
					type = BlogList.TYPE_RECOMMEND;
					break;
				}
				try {
					BlogList list = appContext.getBlogList(type, pageIndex,
							isRefresh);
					msg.what = list.getPageSize();
					msg.obj = list;
				} catch (AppException e) {
					e.printStackTrace();
					msg.what = -1;
					msg.obj = e;
				}
				msg.arg1 = action;
				msg.arg2 = UIHelper.LISTVIEW_DATATYPE_BLOG;
				if (curNewsCatalog == catalog)
					handler.sendMessage(msg);
			}
		}.start();
	}

	/**
	 * 线程加载帖子数据
	 * 
	 * @param catalog
	 *            分类
	 * @param pageIndex
	 *            当前页数
	 * @param handler
	 *            处理器
	 * @param action
	 *            动作标识
	 */
	private void loadLvQuestionData(final int catalog, final int pageIndex,
			final Handler handler, final int action) {
		mHeadProgress.setVisibility(ProgressBar.VISIBLE);
		new Thread() {
			public void run() {
				Message msg = new Message();
				boolean isRefresh = false;
				if (action == UIHelper.LISTVIEW_ACTION_REFRESH
						|| action == UIHelper.LISTVIEW_ACTION_SCROLL)
					isRefresh = true;
				try {
					PostList list = appContext.getPostList(catalog, pageIndex,
							isRefresh);
					msg.what = list.getPageSize();
					msg.obj = list;
				} catch (AppException e) {
					e.printStackTrace();
					msg.what = -1;
					msg.obj = e;
				}
				msg.arg1 = action;
				msg.arg2 = UIHelper.LISTVIEW_DATATYPE_POST;
				if (curQuestionCatalog == catalog)
					handler.sendMessage(msg);
			}
		}.start();
	}

	/**
	 * 线程加载动弹数据
	 * 
	 * @param catalog
	 *            -1 热门，0 最新，大于0 某用户的动弹(uid)
	 * @param pageIndex
	 *            当前页数
	 * @param handler
	 *            处理器
	 * @param action
	 *            动作标识
	 */
	private void loadLvTweetData(final int catalog, final int pageIndex,
			final Handler handler, final int action) {
		mHeadProgress.setVisibility(ProgressBar.VISIBLE);
		new Thread() {
			public void run() {
				Message msg = new Message();
				boolean isRefresh = false;
				if (action == UIHelper.LISTVIEW_ACTION_REFRESH
						|| action == UIHelper.LISTVIEW_ACTION_SCROLL)
					isRefresh = true;
				try {
					TweetList list = appContext.getTweetList(catalog,
							pageIndex, isRefresh);
					msg.what = list.getPageSize();
					msg.obj = list;
				} catch (AppException e) {
					e.printStackTrace();
					msg.what = -1;
					msg.obj = e;
				}
				msg.arg1 = action;
				msg.arg2 = UIHelper.LISTVIEW_DATATYPE_TWEET;
				if (curTweetCatalog == catalog)
					handler.sendMessage(msg);
			}
		}.start();
	}
	
	/**
	 * 线程加载JB新闻列表数据
	 * 
	 * @param catalog
	 *            暂时无用
	 * @param pageIndex
	 *            当前页数
	 * @param handler
	 *            处理器
	 * @param action
	 *            动作标识
	 */
	private void loadLvJBNewsData(final int catalog, final int pageIndex,
			final Handler handler, final int action) {
		
		mHeadProgress.setVisibility(ProgressBar.VISIBLE);
		
		new Thread() {
			public void run() {
				Message msg = new Message();
				boolean isRefresh = false;
				if (action == UIHelper.LISTVIEW_ACTION_REFRESH
						|| action == UIHelper.LISTVIEW_ACTION_SCROLL
						|| action == UIHelper.LISTVIEW_ACTION_CHANGE_CATALOG 
						|| action == UIHelper.LISTVIEW_ACTION_INIT) { //增加当点击顶部切换类别时候也需要刷新列表
					//hasRefreshed = true;
					isRefresh = true;
				}
				if (action == UIHelper.LISTVIEW_ACTION_AUTO_REFRESH) {//自动刷新
					isRefresh = true;
				}
				
				try {
					JBNewsList list = appContext.getJBNewsList(catalog, pageIndex, isRefresh);
					msg.what = list.getPageSize();
					msg.obj = list;
				} catch (AppException e) {
					e.printStackTrace();
					msg.what = -1;
					msg.obj = e;
				}
				//isRefreshing = false;
				msg.arg1 = action;
				msg.arg2 = UIHelper.LISTVIEW_DATATYPE_JBNEWS;
				if (action == UIHelper.LISTVIEW_ACTION_AUTO_REFRESH) {
					handler.sendMessage(msg);
				} else {
					if (curJBNewsCatalog == catalog)
						handler.sendMessage(msg);
				}
			}
		}.start();
	}
	
	/**
	 * 线程加载流程实例列表数据
	 * 
	 * @param catalog
	 *            1 运行实例，2 历史实例
	 * @param pageIndex
	 *            当前页数
	 * @param handler
	 *            处理器
	 * @param action
	 *            动作标识
	 */
	private void loadLvInstData(final int catalog, final int pageIndex,
			final Handler handler, final int action) {
		
		mHeadProgress.setVisibility(ProgressBar.VISIBLE);
		
		new Thread() {
			public void run() {
				Message msg = new Message();
				boolean isRefresh = false;
				if (action == UIHelper.LISTVIEW_ACTION_REFRESH
						|| action == UIHelper.LISTVIEW_ACTION_SCROLL
						|| action == UIHelper.LISTVIEW_ACTION_CHANGE_CATALOG 
						|| action == UIHelper.LISTVIEW_ACTION_INIT) { //增加当点击顶部切换类别时候也需要刷新列表
					//hasRefreshed = true;
					isRefresh = true;
				}
				if (action == UIHelper.LISTVIEW_ACTION_AUTO_REFRESH) {//自动刷新
					isRefresh = true;
				}
				
				try {
					InstanceList list = appContext.getRunningList(catalog, pageIndex, isRefresh);
					msg.what = list.getPageSize();
					msg.obj = list;
				} catch (AppException e) {
					e.printStackTrace();
					msg.what = -1;
					msg.obj = e;
				}
				//isRefreshing = false;
				msg.arg1 = action;
				msg.arg2 = UIHelper.LISTVIEW_DATATYPE_INST;
				if (action == UIHelper.LISTVIEW_ACTION_AUTO_REFRESH) {
					handler.sendMessage(msg);
				} else {
					if (curInstCatalog == catalog)
						handler.sendMessage(msg);
				}
			}
		}.start();
	}
	
	/**
	 * 线程加载todoTask列表数据
	 * 
	 * @param catalog
	 *            1 待签收任务，2 待办任务，3 所有任务
	 * @param pageIndex
	 *            当前页数
	 * @param handler
	 *            处理器
	 * @param action
	 *            动作标识
	 */
	private void loadLvTodoTaskData(final int catalog, final int pageIndex,
			final Handler handler, final int action) {
		
		if (isRefreshing) {
			return;
		} else {
			isRefreshing = true;
		}
		
		mHeadProgress.setVisibility(ProgressBar.VISIBLE);
		
		new Thread() {
			public void run() {
				Message msg = new Message();
				boolean isRefresh = false;
				if (action == UIHelper.LISTVIEW_ACTION_REFRESH
						|| action == UIHelper.LISTVIEW_ACTION_SCROLL
						|| action == UIHelper.LISTVIEW_ACTION_CHANGE_CATALOG 
						|| action == UIHelper.LISTVIEW_ACTION_INIT) { //增加当点击顶部切换类别时候也需要刷新列表
					hasRefreshed = true;
					isRefresh = true;
				}
				if (action == UIHelper.LISTVIEW_ACTION_AUTO_REFRESH) {//自动刷新
					isRefresh = true;
				}
				
				try {
					TodoList list = appContext.getTodoList(catalog,
							pageIndex, isRefresh);
					//msg.what = list.getPageSize();
					msg.what = list.getTodolist().size();
					msg.obj = list;
				} catch (AppException e) {
					e.printStackTrace();
					msg.what = -1;
					msg.obj = e;
				}
				isRefreshing = false;
				msg.arg1 = action;
				msg.arg2 = UIHelper.LISTVIEW_DATATYPE_TODO;
				
				if (action == UIHelper.LISTVIEW_ACTION_AUTO_REFRESH) {
					handler.sendMessage(msg);
				} else if (curTodoCatalog == catalog) {
					handler.sendMessage(msg);
				}
				

			}
		}.start();
	}

	/**
	 * 线程加载动态数据
	 * 
	 * @param catalog
	 * @param pageIndex
	 *            当前页数
	 * @param handler
	 * @param action
	 */
	private void loadLvActiveData(final int catalog, final int pageIndex,
			final Handler handler, final int action) {
		mHeadProgress.setVisibility(ProgressBar.VISIBLE);
		new Thread() {
			public void run() {
				Message msg = new Message();
				boolean isRefresh = false;
				if (action == UIHelper.LISTVIEW_ACTION_REFRESH
						|| action == UIHelper.LISTVIEW_ACTION_SCROLL)
					isRefresh = true;
				try {
					ActiveList list = appContext.getActiveList(catalog,pageIndex, isRefresh);
					msg.what = list.getPageSize();
					msg.obj = list;
				} catch (AppException e) {
					e.printStackTrace();
					msg.what = -1;
					msg.obj = e;
				}
				msg.arg1 = action;
				msg.arg2 = UIHelper.LISTVIEW_DATATYPE_ACTIVE;
				if (curActiveCatalog == catalog)
					handler.sendMessage(msg);
			}
		}.start();
	}

	/**
	 * 线程加载留言数据
	 * 
	 * @param pageIndex
	 *            当前页数
	 * @param handler
	 * @param action
	 */
	private void loadLvMsgData(final int pageIndex, final Handler handler,
			final int action) {
		mHeadProgress.setVisibility(ProgressBar.VISIBLE);
		new Thread() {
			public void run() {
				Message msg = new Message();
				boolean isRefresh = false;
				if (action == UIHelper.LISTVIEW_ACTION_REFRESH
						|| action == UIHelper.LISTVIEW_ACTION_SCROLL)
					isRefresh = true;
				try {
					MessageList list = appContext.getMessageList(pageIndex,
							isRefresh);
					msg.what = list.getPageSize();
					msg.obj = list;
				} catch (AppException e) {
					e.printStackTrace();
					msg.what = -1;
					msg.obj = e;
				}
				msg.arg1 = action;
				msg.arg2 = UIHelper.LISTVIEW_DATATYPE_MESSAGE;
				handler.sendMessage(msg);
			}
		}.start();
	}

	/**
	 * 轮询通知信息
	 */
	private void foreachUserNotice() {
		final int uid = appContext.getLoginUid();
		final Handler handler = new Handler() {
			public void handleMessage(Message msg) {
				if (msg.what == 1) {
					UIHelper.sendBroadCast(Main.this, (Notice) msg.obj);
				}
				foreachUserNotice();// 回调
			}
		};
		new Thread() {
			public void run() {
				Message msg = new Message();
				try {
					sleep(60 * 1000);
					if (uid > 0) {
						Notice notice = appContext.getUserNotice(uid);
						msg.what = 1;
						msg.obj = notice;
					} else {
						msg.what = 0;
					}
				} catch (AppException e) {
					e.printStackTrace();
					msg.what = -1;
				} catch (Exception e) {
					e.printStackTrace();
					msg.what = -1;
				}
				handler.sendMessage(msg);
			}
		}.start();
	}
	
	/**
	 * 轮询通知信息
	 */
	private void foreachTodoTask() {
		final String uid = appContext.getLoginAccount();
		final Handler handler = new Handler() {
			public void handleMessage(Message msg) {
				if (msg.what == 1) {
					
					loadLvTodoTaskData(TodoTask.CATALOG_CLAIM, 0, lvTodoTaskHandler, UIHelper.LISTVIEW_ACTION_AUTO_REFRESH);
					
					//UIHelper.sendBroadCast(Main.this, (Notice) msg.obj);
				}
				foreachTodoTask();// 回调
			}
		};
		new Thread() {
			public void run() {
				Message msg = new Message();
				try {
					sleep(60 * 1000);
					if (!StringUtils.isEmpty(appContext.getLoginAccount()) && autoRefresh) {
						if (hasRefreshed) {
							hasRefreshed = false;
							msg.what = 0;
						} else {
							msg.what = 1;
						}
						//Notice notice = appContext.getUserNotice(uid);
						//msg.obj = notice;
					} else {
						msg.what = 0;
					}
				} catch (Exception e) {
					e.printStackTrace();
					msg.what = -1;
				}
				handler.sendMessage(msg);
			}
		}.start();
	}


	/**
	 * 通知信息处理
	 * 
	 * @param type
	 *            1:@我的信息 2:未读消息 3:评论个数 4:新粉丝个数
	 */
	private void ClearNotice(final int type) {
		final int uid = appContext.getLoginUid();
		final Handler handler = new Handler() {
			public void handleMessage(Message msg) {
				if (msg.what == 1 && msg.obj != null) {
					Result res = (Result) msg.obj;
					if (res.OK() && res.getNotice() != null) {
						UIHelper.sendBroadCast(Main.this, res.getNotice());
					}
				} else {
					((AppException) msg.obj).makeToast(Main.this);
				}
			}
		};
		new Thread() {
			public void run() {
				Message msg = new Message();
				try {
					Result res = appContext.noticeClear(uid, type);
					msg.what = 1;
					msg.obj = res;
				} catch (AppException e) {
					e.printStackTrace();
					msg.what = -1;
					msg.obj = e;
				}
				handler.sendMessage(msg);
			}
		}.start();
	}

	/**
	 * 创建menu TODO 停用原生菜单
	 */
	public boolean onCreateOptionsMenu(Menu menu) {
		return false;
	}

	/**
	 * 菜单被显示之前的事件
	 */
	public boolean onPrepareOptionsMenu(Menu menu) {
		UIHelper.showMenuLoginOrLogout(this, menu);
		return true;
	}

	/**
	 * 处理menu的事件
	 */
	public boolean onOptionsItemSelected(MenuItem item) {
		int item_id = item.getItemId();
		switch (item_id) {
		case R.id.main_menu_user:
			UIHelper.loginOrLogout(this);
			break;
		case R.id.main_menu_about:
			UIHelper.showAbout(this);
			break;
		case R.id.main_menu_setting:
			UIHelper.showSetting(this);
			break;
		case R.id.main_menu_exit:
			UIHelper.Exit(this);
			break;
		}
		return true;
	}

	/**
	 * 监听返回--是否退出程序
	 */
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		boolean flag = true;
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			// 是否退出应用
			return mDoubleClickExitHelper.onKeyDown(keyCode, event);
		} else if (keyCode == KeyEvent.KEYCODE_MENU) {
			// 展示快捷栏&判断是否登录
			UIHelper.showSettingLoginOrLogout(Main.this,
					mGrid.getQuickAction(0));
			mGrid.show(fbSetting, true);
		} else if (keyCode == KeyEvent.KEYCODE_SEARCH) {
			// TODO: 展示搜索页
			//UIHelper.showSearch(Main.this);
		} else {
			flag = super.onKeyDown(keyCode, event);
		}
		return flag;
	}
	
	@Override 
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{ 	
		if (resultCode != RESULT_OK) return;   
    	//if (data == null) return;
    	
        if (requestCode == UIHelper.REQUEST_CODE_FOR_RESULT) 
        {
        	loadLvTodoTaskData(curTodoCatalog, 0, lvTodoTaskHandler, UIHelper.LISTVIEW_ACTION_REFRESH);
        } else if (requestCode == UIHelper.REQUEST_CODE_FOR_REFRESH_INST) {
        	loadLvInstData(curInstCatalog, 0, lvInstHandler, UIHelper.LISTVIEW_ACTION_REFRESH);
        }
	}
}
